Java session.connection() 在 Hibernate 上被弃用了?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3526556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
session.connection() deprecated on Hibernate?
提问by TraderJoeChicago
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.
我们需要能够获得java.sql.Connection
休眠会话的关联。没有其他连接可以工作,因为此连接可能与正在运行的事务相关联。
If session.connection() is now deprecated, how am I supposed to do that?
如果 session.connection() 现在已弃用,我该怎么做?
采纳答案by KeatsPeeks
You now have to use the Work API:
您现在必须使用工作 API:
session.doWork(
new Work() {
public void execute(Connection connection) throws SQLException
{
doSomething(connection);
}
}
);
Or, in Java 8+ :
或者,在 Java 8+ 中:
session.doWork(connection -> doSomething(connection));
回答by Pascal Thivent
If
session.connect()
is now deprecated, how am I supposed to do that?
如果
session.connect()
现在已弃用,我该怎么做?
You have to use Session#doWork(Work)
and the Work
API, as mentioned in the Javadoc:
你必须使用Session#doWork(Work)
和Work
API,如Javadoc中提到:
connection()
Deprecated.(scheduled for removal in 4.x). Replacement depends on need; for doing direct JDBC stuff usedoWork(org.hibernate.jdbc.Work)
; for opening a 'temporary Session' use (TBD).
connection()
已弃用。(计划在 4.x 中删除)。更换取决于需要;用于直接使用 JDBC 的东西doWork(org.hibernate.jdbc.Work)
;用于打开“临时会话”使用 (TBD)。
You have some time before Hibernate 4.x but, well, using a deprecated API somehow looks like this:
您在 Hibernate 4.x 之前还有一段时间,但是,以某种方式使用已弃用的 API 看起来像这样:
:)
:)
Update:According to RE: [hibernate-dev] Connection proxyingon the hibernate-dev list, it seems that the initial intention of the deprecation was to discourage the use of Session#connection()
because it was/is considered as a "bad" API, but it was supposed to stay at that time. I guess they changed their mind...
更新:根据RE:[hibernate-dev] 连接代理在 hibernate-dev 列表中,似乎弃用的最初意图是阻止使用,Session#connection()
因为它被认为是/被认为是“坏”API,但它应该留在那个时候。我猜他们改变了主意......
回答by Patrick
connection()
was just deprecated on the interface. It is still available on SessionImpl
. You can do what Spring does and just call that one.
connection()
只是在界面上被弃用。它仍然可以在SessionImpl
. 您可以执行 Spring 所做的操作,并直接调用它。
Here is the code from HibernateJpaDialect
in Spring 3.1.1
这是HibernateJpaDialect
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);
}
}
回答by Stefan Haberl
There's another option with still a lot of casts involved, but at least it doesn't need reflection, which will give you back compile time checking:
还有另一种选择,仍然涉及很多强制转换,但至少它不需要反射,这将使您返回编译时检查:
public Connection getConnection(final EntityManager em) {
HibernateEntityManager hem = (HibernateEntityManager) em;
SessionImplementor sim = (SessionImplementor) hem.getSession();
return sim.connection();
}
You could of course make that even "prettier" with a few instanceof
checks, but the version above works for me.
你当然可以通过一些instanceof
检查让它变得“更漂亮” ,但上面的版本对我有用。
回答by user2209871
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();
}
}
回答by Mohammad Hosseini
Try This
尝试这个
((SessionImpl)getSession()).connection()
Actuly getSession returns Session Interface type, you should see what is the original class for the session, type cast to the original class then get the connection.
实际上 getSession 返回的是 Session Interface 类型,您应该看到会话的原始类是什么,将类型转换为原始类然后获取连接。
GOOD LUCK!
祝你好运!
回答by segaurav
Try this:
尝试这个:
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;
}
回答by shcherbak
For hibenate 4.3 try this:
对于 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;
}
回答by Louis-Félix
Here is a way to do it in Hibernate 4.3, and it is not deprecated:
这是在 Hibernate 4.3 中执行此操作的一种方法,并且未弃用:
Session session = entityManager.unwrap(Session.class);
SessionImplementor sessionImplementor = (SessionImplementor) session;
Connection conn = sessionImplementor.getJdbcConnectionAccess().obtainConnection();
回答by Simon Mbatia
This is what I use and works for me. Downcast the Session object into a SessionImpl and get the connection object easily:
这就是我使用的并且对我有用。将 Session 对象向下转换为 SessionImpl 并轻松获取连接对象:
SessionImpl sessionImpl = (SessionImpl) session;
Connection conn = sessionImpl.connection();
where session
is the name of your Hibernate session object.
session
您的 Hibernate 会话对象的名称在哪里。