hibernate jpa 2.0 报错Hibernate cannot unwrap interface java.sql.Connection

今天在做报表的时候,利用Hibernate JPA 2.0需要获取数据库连接com.sql.Connection的时候获取不到,网上说用这种方式解决:

entityManager.getTransaction().begin();
java.sql.Connection connection = entityManager.unwrap(java.sql.Connection.class); // unwraps the Connection class.
...
entityManager.getTransaction().commit();
但是各种尝试,总是报出错误: Hibernate cannot unwrap interface java.sql.Connection 
 最后这种方式正确:
 
Connection connection = entityManager.unwrap(SessionImpl.class).connection();
这样成功获取数据库连接Connection.
 
原文地址:https://www.cnblogs.com/Cilimer/p/4074518.html