继承以下类即可:

import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.SessionHolder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.support.TransactionSynchronizationManager;

/**
 * Title: AbstractSessionTestCase.java
 */
@RunWith(SpringJUnit4ClassRunner.class)   
@ContextConfiguration(locations={"/spring.xml"})
public abstract class AbstractSessionTestCase {
    
    @Autowired
    private SessionFactory sessionFactory;
    private Session session;
    
    @Before
    public void openSession()  throws Exception {
        session = SessionFactoryUtils.getSession(sessionFactory, true);
        session.setFlushMode(FlushMode.MANUAL);
        TransactionSynchronizationManager.bindResource(sessionFactory,new SessionHolder(session));
    }
    
    @After
    public void closeSession()  throws Exception {
        TransactionSynchronizationManager.unbindResource(sessionFactory);
        SessionFactoryUtils.releaseSession(session, sessionFactory);
    }

}

 

相关文章:

  • 2021-12-04
  • 2021-06-14
  • 2022-12-23
  • 2021-08-07
  • 2021-08-04
  • 2021-12-03
猜你喜欢
  • 2022-12-23
  • 2021-11-23
  • 2021-09-08
  • 2021-11-10
  • 2021-10-01
  • 2021-05-19
  • 2022-12-23
相关资源
相似解决方案