在我的前面的一篇文章里,介绍了如何用OpenSessionInViewInterceptor 的解决方案,但这种方案是在web环境下才能解决,如果我们在非web环境如何解决呢?先介绍一个TestCase的解决方案吧:
import junit.framework.TestCase;
import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.SessionHolder;
import org.springframework.transaction.support.TransactionSynchronizationManager;
public abstract class AbstractTestBean extends TestCase {
protected ApplicationContext applicationContext;
private SessionFactory sessionFactory;
private Session session;
protected void setUp() throws Exception {
String configFile = "spring/*.xml";
applicationContext = new ClassPathXmlApplicationContext(configFile);
sessionFactory = (SessionFactory) applicationContext.getBean("sessionFactory");
session = SessionFactoryUtils.getSession(sessionFactory, true);
session.setFlushMode(FlushMode.NEVER);
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
}
protected void tearDown() throws Exception {
TransactionSynchronizationManager.unbindResource(sessionFactory);
SessionFactoryUtils.releaseSession(session, sessionFactory);
}
}
import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.SessionHolder;
import org.springframework.transaction.support.TransactionSynchronizationManager;
public abstract class AbstractTestBean extends TestCase {
protected ApplicationContext applicationContext;
private SessionFactory sessionFactory;
private Session session;
protected void setUp() throws Exception {
String configFile = "spring/*.xml";
applicationContext = new ClassPathXmlApplicationContext(configFile);
sessionFactory = (SessionFactory) applicationContext.getBean("sessionFactory");
session = SessionFactoryUtils.getSession(sessionFactory, true);
session.setFlushMode(FlushMode.NEVER);
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
}
protected void tearDown() throws Exception {
TransactionSynchronizationManager.unbindResource(sessionFactory);
SessionFactoryUtils.releaseSession(session, sessionFactory);
}
}
这是我的TestCase的基类,所有的TestCase继承这个类,就可以具备OpenSessionInView的功能。
示例代码如下:
public class TestDB extends AbstractTestBean {
@Test
public void test() {
ChargeinfoDAO chargeinfoDAO = (ChargeinfoDAO) applicationContext
.getBean("ChargeinfoDAO");
Set tasks = chargeinfo.getTasks();
for (Iterator it = tasks.iterator(); it.hasNext();) {
Task task = (Task) it.next();
System.out.println(task.getSvName());
}
@Test
public void test() {
ChargeinfoDAO chargeinfoDAO = (ChargeinfoDAO) applicationContext
.getBean("ChargeinfoDAO");
Chargeinfo chargeinfo
= chargeinfoDAO.findById(new Long(3652));Set tasks = chargeinfo.getTasks();
for (Iterator it = tasks.iterator(); it.hasNext();) {
Task task = (Task) it.next();
System.out.println(task.getSvName());
}
}
}