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

请教关于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
    wuwenming101 2008-05-31 17:23
    已采纳

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

    点赞 评论
  • arlo_111
    arlo_111 2008-05-23 23:40

    哎··哥们!
    index这个属性名称改一下
    关键字啊!!!
    改成其他,这种问题不应该发生的

    点赞 评论

相关推荐