org.hibernate.exception.SQLGrammarException: could not fetch initial value for increment generator

以下是我的映射文件
Order.hbm.xml





<hibernate-mapping>  
    <class name="cn.itcast.hibernate0909.onetomany.test.Order" >  
        <id name="oid" type="integer">  
            <column name="id">column>  
            <generator class="increment">generator>  
        id>  

        <property name="orderNumber" type="string" >  property>  
        <property name="price" type="double"> property>  

        <many-to-one name="customer" class="cn.itcast.hibernate0909.onetomany.test.Customer" >  
            <column name="customer_id" >column>     
        many-to-one>  
    class>  
hibernate-mapping>  

Customer.hbm.xml





<hibernate-mapping>  
<class name="cn.itcast.hibernate0909.onetomany.test.Customer" table="customers" >  
    <id name="cid" type="integer">  
        <column name="cid">column>  
        <generator class="increment">generator>  
    id>  
    <property name="name" type="string" >  
    property>  
class>  
hibernate-mapping>  

org.hibernate.exception.SQLGrammarException: could not fetch initial value for increment generator_第1张图片

junit测试发现报错:
org.hibernate.exception.SQLGrammarException: could not fetch initial value for increment generator_第2张图片

org.hibernate.exception.SQLGrammarException: could not fetch initial value for increment generator_第3张图片

在这里的错误是使用了Mysql中的关键字’order’,再配置Order.hbm.xml文件时候,不设置表明,默认使用持久化类的类名order造成报错。
解决方法:设置表名
org.hibernate.exception.SQLGrammarException: could not fetch initial value for increment generator_第4张图片
重新junit测试:
org.hibernate.exception.SQLGrammarException: could not fetch initial value for increment generator_第5张图片

错误总结:
Hibernate: select max(‘主键名’) from ‘表名’
org.hibernate.exception.SQLGrammarException: could not fetch initial value for increment generator
解决方案:
1.查看一下表名是否与数据库中的表名相同,对于某些数据库,大小写是要区分的。
2.查看表名或者字段使用了数据库中的关键字

你可能感兴趣的:(Hibernate,SSH框架)