yuexiaqingfeng 2008-05-11 16:27
浏览 143
已采纳

请教关于Hibernate的一个问题!

近来做毕业设计,使用hibernate和mysql数据库。在插入数据时出现如下问题:
异常1:
[code="java"]
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
[/code]
异常2:
[code="java"]
org.hibernate.exception.SQLGrammarException: could not insert: [qau.cookbook.hibernate.po.basedata.Foodkindctrl]
[/code]
下面是我的数据库表的映射文件:
[code="xml"]








































[/code]
下面是pojo类文件:
[code="java"]
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;   
}   

}

[/code]
下面是我写的插入函数:
[code="java"]
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();

}

}

[/code]
请问各位高手为什么会出现这样的问题,该如何解决呢?

  • 写回答

2条回答 默认 最新

  • wuwenming101 2008-05-31 17:23
    关注

    index是数据库的关键字, 把这个属性换掉...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?