`
winyou365
  • 浏览: 9019 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

请教关于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 class="identity" />
        </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 class="qau.cookbook.hibernate.po.basedata.Foodkinddtl" />
        </set>
    </class>
</hibernate-mapping>

下面是pojo类文件:
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();
	  }
   }

请问各位高手为什么会出现这样的问题,该如何解决呢?
分享到:
评论
2 楼 winyou365 2008-05-20  
谢谢楼上的回答,问题确实出在这个地方,前几天刚刚解决。
1 楼 myali88 2008-05-20  
index在MySQL里是关键字,属性index对应的列名不要用index

相关推荐

Global site tag (gtag.js) - Google Analytics