AymanKun
AymanKun

Reputation: 241

Hibernate error on update : could not execute statement, the right syntax to use near 'index=1'

I'm developing a desktop application using netbeans with Hibernate and MySQL . When I add a new record to the database it's totally working but when I try to update an object field I have this error :

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 code :

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 mapping 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>

Upvotes: 4

Views: 3174

Answers (1)

Vlad Mihalcea
Vlad Mihalcea

Reputation: 154090

INDEX is a reserved word in MySQL, and in most other relational database systems.

You need to rename the identifier to id.

Upvotes: 3

Related Questions