请教关于Hibernate的一个问题!
近来做毕业设计,使用hibernate和mysql数据库。在插入数据时出现如下问题:
异常1:
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:
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) values ('123', 4)' at line 1
异常2:
org.hibernate.exception.SQLGrammarException: could not insert: [qau.cookbook.hibernate.po.basedata.Foodkindctrl]
下面是我的数据库表的映射文件:
<hibernate-mapping> <class name="qau.cookbook.hibernate.po.basedata.Foodkindctrl" table="foodkindctrl" catalog="cookbook"> <id name="id" type="integer"> <column name="id" /> <generator /> </id> <property name="foodkindctrlname" type="string"> <column name="foodkindctrlname" length="20" /> </property> <property name="index" type="integer"> <column name="index" /> </property> <set name="foodkinddtls" inverse="true"> <key> <column name="foodkindctrlid" /> </key> <one-to-many /> </set> </class></hibernate-mapping>
public class Foodkindctrl implements java.io.Serializable { // Fields private Integer id; private String foodkindctrlname; private Integer index; private Set foodkinddtls = new HashSet(0); // Constructors /** default constructor */ public Foodkindctrl() { } /** full constructor */ public Foodkindctrl(String foodkindctrlname, Integer index, Set foodkinddtls) { this.foodkindctrlname = foodkindctrlname; this.index = index; this.foodkinddtls = foodkinddtls; } // Property accessors public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getFoodkindctrlname() { return this.foodkindctrlname; } public void setFoodkindctrlname(String foodkindctrlname) { this.foodkindctrlname = foodkindctrlname; } public Integer getIndex() { return this.index; } public void setIndex(Integer index) { this.index = index; } public Set getFoodkinddtls() { return this.foodkinddtls; } public void setFoodkinddtls(Set foodkinddtls) { this.foodkinddtls = foodkinddtls; } }
public boolean saveKindCtrl(Foodkindctrl kindCtrl){ Transaction tx=null; try{ Session session = HibernateSessionFactory.getSession(); tx = session.beginTransaction(); session.save(kindCtrl); tx.commit(); log.debug("保存成功"); return true; }catch(Exception e){ e.printStackTrace(); log.debug("保存失败"); return false; }finally{ HibernateSessionFactory.closeSession(); } }