mysql - Session 类型的方法connection() 未定义

标签 mysql hibernate

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/

相关文章:

mysql - 如何永久禁用外键检查

java - 值在提交时发生变化。 |可调用语句

php - 比较每行数据的表字段并采取一些措施 (PHP)

java - 在 hibernate 中选择

mysql - Hibernate 按子表的 Id 排序

hibernate - 有没有办法测试我的grails应用程序的HQL查询?

sql - 查找和替换多条记录中的数据

mysql - 正确加入1 :n:1:1 relation in mysql database

使用 log4j 进行 Hibernate 日志记录

java - Hibernate:设置固定参数是一个好习惯吗?