session.connection() deprecated on Hibernate?

TraderJoeChicago picture TraderJoeChicago · Aug 19, 2010 · Viewed 83.6k times · Source

We need to be able to get the associated java.sql.Connection of a hibernate session. No other connection will work, as this connection may be associated with a running transaction.

If session.connection() is now deprecated, how am I supposed to do that?

Answer

KeatsPeeks picture KeatsPeeks · Aug 19, 2010

You now have to use the Work API:

session.doWork(
    new Work() {
        public void execute(Connection connection) throws SQLException 
        { 
            doSomething(connection); 
        }
    }
);

Or, in Java 8+ :

session.doWork(connection -> doSomething(connection));