从spring中取得hibernate的session及SessionFactory


import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.hibernate.Session;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springside.modules.test.spring.SpringTransactionalTestCase;

@TransactionConfiguration(defaultRollback = false)
@ContextConfiguration(locations = { "/applicationContext.xml" })
public class RetriveSessionFactoryFromEntityManager extends SpringTransactionalTestCase {
	@PersistenceContext
	private EntityManager em;

	@Test
	public void testFuWuBaoJia() throws Exception {
		{
			Session session = em.unwrap(Session.class);
			System.out.println(session);
			System.out.println(session.getSessionFactory());
		}
		{
			Session session = (Session) em.getDelegate();
			System.out.println(session);
			System.out.println(session.getSessionFactory());
		}

	}
}

你可能感兴趣的:(sessionFactory)