如何从JPA 2.0 EntityManager获得Hibernate Session
展开全部
1、获得Hibernate Session
Session session = entityManager.unwrap(org.hibernate.Session.class);
2、获得java.sql.Connection
方法1:
JPA 2.0
entityManager.getTransaction().begin();
java.sql.Connection connection = entityManager.unwrap(java.sql.Connection.class);
...
entityManager.getTransaction().commit();
但是对于Hibernate3.6.5会报错PersistenceException包含如下信息:Hibernate cannot unwrap interface java.sql.Connection
方法2:
SessionImplementor session =entityManager.unwrap(SessionImplementor.class);
session.connection();
方法3:
Session session = (org.hibernate.Session) em.getDelegate();
SessionFactoryImplementor sf = (SessionFactoryImplementor) session.getSessionFactory();
connection = sf.getConnectionProvider().getConnection();
hibenate 4.3 :
Session session = (Session) em.getDelegate();
SessionFactoryImpl sessionFactory = (SessionFactoryImpl) session.getSessionFactory();
connection = sessionFactory.getConnectionProvider().getConnection();
Session session = entityManager.unwrap(org.hibernate.Session.class);
2、获得java.sql.Connection
方法1:
JPA 2.0
entityManager.getTransaction().begin();
java.sql.Connection connection = entityManager.unwrap(java.sql.Connection.class);
...
entityManager.getTransaction().commit();
但是对于Hibernate3.6.5会报错PersistenceException包含如下信息:Hibernate cannot unwrap interface java.sql.Connection
方法2:
SessionImplementor session =entityManager.unwrap(SessionImplementor.class);
session.connection();
方法3:
Session session = (org.hibernate.Session) em.getDelegate();
SessionFactoryImplementor sf = (SessionFactoryImplementor) session.getSessionFactory();
connection = sf.getConnectionProvider().getConnection();
hibenate 4.3 :
Session session = (Session) em.getDelegate();
SessionFactoryImpl sessionFactory = (SessionFactoryImpl) session.getSessionFactory();
connection = sessionFactory.getConnectionProvider().getConnection();
名片
2024-10-28 广告
2024-10-28 广告
Altair HyperWorks是一套功能强大的集成化CAE(计算机辅助工程)软件平台,由Altair公司开发。它涵盖了结构分析、流体动力学、优化、多体动力学、电磁场仿真等多个领域,提供了丰富的仿真工具和优化算法。HyperWorks以其...
点击进入详情页
本回答由名片提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询