更新时休眠错误:无法执行语句,使用正确的语法接近'index=1'



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

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

爪哇代码:

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

休眠.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>

休眠映射 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是MySQL和大多数其他关系数据库系统中的保留字。

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

最新更新