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.
 
posted @ 2014-11-04 18:52  Cilimer  阅读(1622)  评论(0)    收藏  举报