`
lk557
  • 浏览: 139415 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

HIBERNATE错误集锦

 
阅读更多

1. 错误:Could not parse mapping document from input stream

解决方法:工程中有某个hbm.xml 文件不规范

我的错误在注释某个属性时,没注释完整,遗留了</property>

说明:只要工程有某个hbm.xml不规范,测试时启动工程都会报这样错误

2. 错误:Duplicate class/entitymapping ConnectExtends.model.StockPutWrhBillDO

我的配置如下:

<!--joined-subclass name="connectextends.model.StockPutWrhBillDO"table="stock_put_wrh_bill">

<keycolumn="WRH_BUSINESS_BILL_ID"/>

<propertyname="PUT_WAREHOUSE" column="PUT_WAREHOUSE"/>

<propertyname="PUT_WRH_TYPE" column="PUT_WRH_TYPE"/>

<propertyname="PUT_WRH_DATE" column="PUT_WRH_DATE"/>

<propertyname="OUT_CINAMA_CODE" column="OUT_CINAMA_CODE"/>

</joined-subclass-->

解决方法:修改对应类的路径

说明:只要工程有某个hbm.xml不规范找不到对应类,测试时启动工程都会报这样错误

3. 错误:WRH_BUSINESS_BILL_ID (should be mapped with insert="false"update="false")

解决方法:

<!--property name="WRH_BUSINESS_BILL_ID"type="integer">

<columnname="WRH_BUSINESS_BILL_ID">

<comment></comment>

</column>

</property-->

<many-to-onename="stockWrhBusinessBillDO"

class="relate.model.StockWrhBusinessBillDO"

column="WRH_BUSINESS_BILL_ID"

insert="true"

update="true"/>

解决方法:<property>与<many-to-one>冲突

两个设置都对应了字段WRH_BUSINESS_BILL_ID,保留一个,去掉

<property>即可

注意: 当工程启动,应用了hibernate,hibernate会检查工程中的所有映射文件,只要工程中有某个映射文件不符合映射语法规则,就会抛出异常

4. 错误:invalid mapping

Caused by:org.xml.sax.SAXParseException: The content of element

type "class" must match"(meta*,subselect?,cache?,synchronize*,

comment?,tuplizer*,(id|composite-id),discriminator?,natural-id?,(version|timestamp)?,(property|many-to-one|one-to-one|component|dynamic-component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,((join*,subclass*)|joined-subclass*|union-subclass*),loader?,sql-insert?,sql-update?,sql-delete?,filter*,resultset*,(query|sql-query)*)".

原先hbm.xml 配置:

<joined-subclassname="animal.model.CatDO" table="cat">

<keycolumn="id" />

<property name="catagory"column="catagory" type="java.lang.String"/>

</joined-subclass>

<setname="animaldetailDOs"

table="animaldetail"

inverse="true"

cascade="all">

<key column="animalid"/>

<one-to-manyclass="animaldetail.model.AnimaldetailDO"/>

</set>

解决方法:将<set>标签内容放到<joiner-subclass>前面,即

<set name="animaldetailDOs"

table="animaldetail"

inverse="true"

cascade="all">

<key column="animalid"/>

<one-to-manyclass="animaldetail.model.AnimaldetailDO"/>

</set>

<joined-subclass name="animal.model.CatDO"table="cat">

<keycolumn="id" />

<property name="catagory"column="catagory" type="java.lang.String"/>

</joined-subclass>

说明:每个xml在文档开头设置了文档类型定义(DOCTYPE),标签就要严格按照文档类型定义的顺序使用。

5. 错误:org.hibernate.exception.SQLGrammarException:could not fetch initial value for incrementgenerator

(使用unnion-subclass元素时,父类标识属性生成器用 increment 报错)

解决方法:在MySql数据库中,父类标识属性生成器hilo。

6. 错误:exception is org.hibernate.MappingException: Cannot use identity columnkey generation with <union-subclass> mapping for:subclass.model.UserhistoryDO

(使用unnion-subclass元素时,父类标识属性生成器用 native 报错)

解决方法:在MySql数据库中,父类标识属性生成器hilo。

说明:native是自动去找生成主键的依据,在Oracle中是去找sequence,然后sequence直接用Hibernate_sequence产生oid,不需要进行高低位运算

而在DB2和MySQL等数据库中是去找identity,可能是在找的时候没有找到identity吧

7. 错误:org.hibernate.exception.SQLGrammarException: could not get or update nextvalue

(使用unnion-subclass元素时,父类标识属性生成器用 hilo,但没有建立对应的id生成表)

解决方法:

1.<id name="id" column="id"type="java.lang.Integer">

<generator class="hilo">

<paramname="table">my_unique_key</param>

<param name="column">next_hi</param>

</generator>

</id>

2. 在数据库中建立表 my_unique_key和字段next_hi ,且next_hi必须有一条记录。

说明:由于Hilo主键生成方式采用的是hilo算法,不必要指定id的(这点和assigned 类似,主键的值由hibernate维护)。但hilo算法需要额外的数据表my_unique_key和字段next_hi(表名和字段名默认是my_unique_key/next_hi,但可以自定义),且next_hi必须有一条记录。

8. 错误:a different object with the same identifier value wasalready associated with the session

错误分析:因为在hibernate中同一个session里面有了两个相同标识但是是不同实体,当这时运行saveOrUpdate(object)操作的时候就会报这个错误

解决方法: 1、a different object with the same identifier value was alreadyassociated with the session。

  错误原因:在hibernate中同一个session里面有了两个相同标识但是是不同实体。

  解决方法一:session.clean()

  PS:如果在clean操作后面又进行了saveOrUpdate(object)等改变数据状态的操作,有可能会报出"Found two representations of same collection"异常。

  解决方法二:session.refresh(object)

  PS:当object不是数据库中已有数据的对象的时候,不能使用session.refresh(object)因为该方法是从hibernate的session中去重新取object,如果session中没有这个对象,则会报错所以当你使用saveOrUpdate(object)之前还需要判断一下。

  解决方法三:session.merge(object)

  PS:Hibernate里面自带的方法,推荐使用。

2、Found two representations of samecollection

  错误原因:见1。

  解决方法:session.merge(object)

以上两中异常经常出现在一对多映射和多对多映射中


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics