Get hold of a JDBC Connection object from a Stateless Bean

后端 未结 7 1837
孤街浪徒
孤街浪徒 2020-12-09 18:43

In a Stateless Session Bean an EntityManager is injected but I would like to get hold of a Connection object in order to invoke a DB Procedure. Is

7条回答
  •  一个人的身影
    2020-12-09 19:18

    The JPA API itself doesn't seem to offer this, not surprisingly, but if you're willing to couple your code to a specific implementation, then you can use something like this (Hibernate):

    Session hibernateSession = entityManager.unwrap(Session.class);
    Connection jdbcConnection = hibernateSession.connection(); 
    

    Note that Session.connection() is deprecated for removal in Hibernate 4. Consider using Session.doWork() instead.

提交回复
热议问题