2015-01-17 27 views
4

我正在開發一個使用帶有Hibernate和MySQL的NetBeans的桌面應用程序。當我添加一個新的記錄到它完全使用的數據庫,但是當我嘗試更新的對象場我有這樣的錯誤:更新時發生Hibernate錯誤:無法執行語句,在'index = 1'附近使用正確的語法

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> 
+0

您可以啓用日誌記錄的休眠生成實際的SQL語句。請參閱http://docs.jboss.org/hibernate/orm/4.3/topical/html/logging/Logging.html – ikettu

回答

3

INDEX is a reserved word在MySQL和大多數其他關係數據庫系統中。

您需要將標識符重命名爲id

+0

是的問題是在ID列名稱「索引」。在我將其更改爲「id」後,它正在工作。 – AymanKun

相關問題