The method connection() is undefined for the type Session
当我在 Hibernate 5.2.2 中使用准备好的语句时,出现此错误。
问题:
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
PreparedStatement statement=session.connection().prepareStatement("INSERT INTO registration values (?,?,?)");
我在连接()中遇到错误。我也更改了 hibernate 版本。但我无法解决此错误。
最佳答案
该方法实际上不存在。它被删除了。 Hibernate 3.5 Javadoc 说:
Deprecated. (scheduled for removal in 4.x). Replacement depends on need; for doing direct JDBC stuff use doWork(org.hibernate.jdbc.Work); for opening a 'temporary Session' use (TBD).
接口(interface)Work
有一个方法来获取传递的Connection
:
void execute(Connection connection) throws SQLException
Execute the discrete work encapsulated by this work instance using the supplied connection.
该方法可以使用 Java 8 lambda 来实现:
session.doWork(() -> {
connection.prepareStatement("INSERT INTO registration values (?,?,?)");
});
关于mysql - Session 类型的方法connection() 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39608800/