今天在做报表的时候,利用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.
 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-26
  • 2022-12-23
  • 2022-01-08
  • 2021-05-16
猜你喜欢
  • 2022-12-23
  • 2021-11-10
  • 2022-12-23
  • 2021-07-23
  • 2021-11-12
  • 2022-12-23
相关资源
相似解决方案