java - 更新 : could not execute statement, 时出现 Hibernate 错误 在 'index=1' 附近使用正确的语法

标签 java mysql sql hibernate orm

我正在使用带有 Hibernate 和 MySQL 的 netbeans 开发桌面应用程序。当我向数据库添加一条新记录时,它完全可以工作,但是当我尝试更新一个对象字段时,出现此错误:

could not execute statement
INFO: HHH000010: On release of batch it still contained JDBC statements
Jan 17, 2015 2:47:00 PM    org.hibernate.engine.jdbc.spi.SqlExceptionHelper$StandardWarningHandler logWarning
WARN: SQL Warning Code: 1064, SQLState: 42000
Jan 17, 2015 2:47:00 PM    org.hibernate.engine.jdbc.spi.SqlExceptionHelper$StandardWarningHandler  logWarning
WARN: You have an error in your SQL syntax; check the manual that   corresponds to your MySQL server version for the right syntax to use near 'index=1' at line 1

Java 代码:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    Session session = null;


    try {
        session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();

        Student student = (Student) session.get(Student.class, new Integer(1));
        student.setMark(0);
        System.out.println(student.getIndex()+" "+student.getName()+" "+student.getMark());

        session.saveOrUpdate(student);
        session.getTransaction().commit();
        session.close();

    } catch (Exception e) {
        System.out.println(e.getMessage());
    } 

}       

hibernate .cfg.xml :

<hibernate-configuration>
   <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/studentsdb?zeroDateTimeBehavior=convertToNull</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <mapping resource="entity/Student.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

Hibernate 映射 xml:

<hibernate-mapping>
<class name="entity.Student" table="student" catalog="studentsdb">
    <id name="index" type="java.lang.Integer">
        <column name="index" />
        <generator class="identity" />
    </id>
    <property name="name" type="string">
        <column name="name" length="20" not-null="true" />
    </property>
    <property name="surname" type="string">
        <column name="surname" length="20" not-null="true" />
    </property>
    <property name="mark" type="double">
        <column name="mark" precision="22" scale="0" not-null="true" />
    </property>
</class>
</hibernate-mapping>

最佳答案

INDEX is a reserved word在 MySQL 和大多数其他关系数据库系统中。

您需要将标识符重命名为id

关于java - 更新 : could not execute statement, 时出现 Hibernate 错误 在 'index=1' 附近使用正确的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28000223/

相关文章:

Java 下载 html

java - Tomcat:HTTP 状态 404

mysql - 从多个表中插入数据

sql - 在 T-SQL 查询中,仅需要获取根元素的属性名称-值对

sql - 从表中获取唯一数据

php - mysqli_query 不运行查询?

java - 将 html datetime-local 类型输入解析为 java LocalDateTime

Java:序列化是否还编写方法代码以流式传输 Hibernate 实体?

PHP/MySQL 安全问题

php - 克隆 Dbal 连接以在重复的 Get_Affected_Rows 上执行插入操作