session.connection()在Hibernate上被弃用?

我们需要能够获得一个hibernate会话的关联java.sql.Connection 。 没有其他连接可以工作,因为此连接可能与正在运行的事务相关联。

如果session.connection()现在被弃用,我该怎么做?

您现在必须使用Work API:

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

或者,在Java 8中:

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

如果session.connect()现在被弃用,我该怎么做?

您必须使用Session#doWork(Work)Work API,如Javadoc中所述:

connection()
已过时。 (计划在4.x中删除)。 更换取决于需要; 对于做直接JDBC的东西使用doWork(org.hibernate.jdbc.Work) ; 开放“临时会议”使用(TBD)。

在Hibernate 4.x之前,你有一些时间,但是,使用不赞成使用的API看起来是这样的:

替代文字 🙂

更新:根据RE:[hibernate-dev]在hibernate-dev列表上的连接代理 ,似乎弃用的初衷是阻止使用Session#connection()因为它被认为是“bad “API,但它应该停留在那个时候。 我想他们改变了主意

还有一个select,还有很多涉及到的强制转换,但至less不需要reflection,这会给你回编译时间检查:

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

你当然可以通过一些instanceof检查来使它更“漂亮”,但是上面的版本适用于我。

下面是在Hibernate 4.3中做的一个方法,不推荐使用:

  Session session = entityManager.unwrap(Session.class); SessionImplementor sessionImplementor = (SessionImplementor) session; Connection conn = sessionImplementor.getJdbcConnectionAccess().obtainConnection(); 

connection()仅在接口上被弃用。 它仍然在SessionImpl可用。 你可以做Spring所做的,只是叫它。

这里是Spring 3.1.1中HibernateJpaDialect的代码

 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); } } 

尝试这个

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

实际上,getSession返回会话接口types,你应该看到什么是会话的原始类,types转换到原始类然后获得连接。

祝你好运!

我发现这篇文章

 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(); } } } 

它帮助了我。

这是我使用和为我工作。 将Session方法下载到SessionImpl中并轻松获取连接对象:

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

其中session是Hibernate会话对象的名称。

Hibernate > = 5.0时,你可以像这样获得Connection

 Connection c = sessionFactory. getSessionFactoryOptions().getServiceRegistry(). getService(ConnectionProvider.class).getConnection(); 

对于hibenate 4.3试试这个:

 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; } 

尝试这个:

 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; } 
  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(); } }