Session.connection () not recommended in Hibernate?

We need to be able to bind a java.sql.Connection a sleep session. No other connection will work, as this connection may be associated with the current transaction.

If session.connection () is now deprecated, how should I do this?

+66
java orm hibernate
Aug 19 '10 at 21:49
source share
12 answers

Now you need 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)); 
+77
Aug 19 '10 at 21:58
source share
— -

If session.connect() now deprecated, how should I do it?

You should use Session#doWork(Work) and the Work API, as stated in Javadoc:

connection()
Outdated. (scheduled for removal in 4.x). Replacement depends on need; to use direct JDBC material, use doWork(org.hibernate.jdbc.Work) ; to open the use of the "temporary session" (TBD).

You have time before Hibernate 4.x, but using an outdated API, it looks something like this:

alt text :)

Update: According to RE: [hibernate-dev] Proxying a connection in the hibernate-dev list it seems that the original intention of the refusal was to refuse to use Session#connection() because it was / is considered a "bad" API, but it should have remained in that time. I think they changed their minds ...

+23
Aug 19 '10 at 23:01
source share

There is another option, in which many tricks are involved, but at least it does not need reflection, which will give you the opportunity to check the compilation time:

 public Connection getConnection(final EntityManager em) { HibernateEntityManager hem = (HibernateEntityManager) em; SessionImplementor sim = (SessionImplementor) hem.getSession(); return sim.connection(); } 

You could even make it “prettier” with a few instanceof checks, but the version above works for me.

+9
Sep 06 2018-12-12T00: 00Z
source share

try it

 ((SessionImpl)getSession()).connection() 

Actuly getSession returns the type of session interface, you should see what is the source class for the session, enter cast in the source class, then get the connection.

LUCK!

+9
May 04 '13 at 2:17
source share

Here is a way to do it in Hibernate 4.3, and it is not deprecated:

  Session session = entityManager.unwrap(Session.class); SessionImplementor sessionImplementor = (SessionImplementor) session; Connection conn = sessionImplementor.getJdbcConnectionAccess().obtainConnection(); 
+9
Dec 10 '14 at 21:34
source share

connection() just deprecated on the interface. It is still available on SessionImpl . You can do what Spring does and just call it.

Here is the code from HibernateJpaDialect in Spring 3.1.1

 public Connection getConnection() { try { if (connectionMethod == null) { // reflective lookup to bridge between Hibernate 3.x and 4.x connectionMethod = this.session.getClass().getMethod("connection"); } return (Connection) ReflectionUtils.invokeMethod(connectionMethod, this.session); } catch (NoSuchMethodException ex) { throw new IllegalStateException("Cannot find connection() method on Hibernate session", ex); } } 
+8
Apr 27 '12 at 22:53
source share

I found this article

 package com.varasofttech.client; import java.sql.Connection; import java.sql.SQLException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.internal.SessionImpl; import org.hibernate.jdbc.ReturningWork; import org.hibernate.jdbc.Work; import com.varasofttech.util.HibernateUtil; public class Application { public static void main(String[] args) { // Different ways to get the Connection object using Session SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); // Way1 - using doWork method session.doWork(new Work() { @Override public void execute(Connection connection) throws SQLException { // do your work using connection } }); // Way2 - using doReturningWork method Connection connection = session.doReturningWork(new ReturningWork<Connection>() { @Override public Connection execute(Connection conn) throws SQLException { return conn; } }); // Way3 - using Session Impl SessionImpl sessionImpl = (SessionImpl) session; connection = sessionImpl.connection(); // do your work using connection // Way4 - using connection provider SessionFactoryImplementor sessionFactoryImplementation = (SessionFactoryImplementor) session.getSessionFactory(); ConnectionProvider connectionProvider = sessionFactoryImplementation.getConnectionProvider(); try { connection = connectionProvider.getConnection(); // do your work using connection } catch (SQLException e) { e.printStackTrace(); } } } 

It helped me.

+5
Apr 14 '16 at 12:30
source share

This is what I use and work for me. Drag and drop the Session method into SessionImpl and easily get the connection object:

 SessionImpl sessionImpl = (SessionImpl) session; Connection conn = sessionImpl.connection(); 

where session is the name of your Hibernate session object.

+4
May 17 '15 at 9:55
source share

With Hibernate > = 5.0, you can get Connection as follows:

 Connection c = sessionFactory. getSessionFactoryOptions().getServiceRegistry(). getService(ConnectionProvider.class).getConnection(); 
+4
May 12 '16 at
source share

For hibenate 4.3, try the following:

 public static Connection getConnection() { EntityManager em = <code to create em>; Session ses = (Session) em.getDelegate(); SessionFactoryImpl sessionFactory = (SessionFactoryImpl) ses.getSessionFactory(); try{ connection = sessionFactory.getConnectionProvider().getConnection(); }catch(SQLException e){ ErrorMsgDialog.getInstance().setException(e); } return connection; } 
+2
Jan 21 '14 at 23:12
source share

Try the following:

 public Connection getJavaSqlConnectionFromHibernateSession() { Session session = this.getSession(); SessionFactoryImplementor sessionFactoryImplementor = null; ConnectionProvider connectionProvider = null; java.sql.Connection connection = null; try { sessionFactoryImplementor = (SessionFactoryImplementor) session.getSessionFactory(); connectionProvider = (ConnectionProvider) sessionFactoryImplementor.getConnectionProvider().getConnection(); connection = connectionProvider.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return connection; } 
+1
Oct 24 '13 at 19:25
source share
  Connection conn = null; PreparedStatement preparedStatement = null; try { Session session = (org.hibernate.Session) em.getDelegate(); SessionFactoryImplementor sfi = (SessionFactoryImplementor) session.getSessionFactory(); ConnectionProvider cp = sfi.getConnectionProvider(); conn = cp.getConnection(); preparedStatement = conn.prepareStatement("Select id, name from Custumer"); ResultSet rs = preparedStatement.executeQuery(); while (rs.next()) { System.out.print(rs.getInt(1)); System.out.println(rs.getString(2)); } } catch (Exception e) { e.printStackTrace(); } finally { if (preparedStatement != null) { preparedStatement.close(); } if (conn != null) { conn.close(); } } 
0
Mar 26 '13 at 2:12
source share



All Articles