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