For some cases, there a need to retrieve jdbc connection to execute some database operations such as batch works; tables loading from big files or others .....
When working with enitity manager
Connection conn = em.unwrap(Session.class).connection();
When working with enitity manager
Connection conn = em.unwrap(Session.class).connection();
raises an exception when working with an hibernate implementation (works fine with eclipselink)
One solution is :
1 2 3 4 5 6 7 8 9 10 11 12 | public Connection hibernateConnection() { SessionImplementor si = (SessionImplementor) entityManager.unwrap(Session. class ); try { return si.getJdbcConnectionAccess().obtainConnection(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return null ; } } |