通過調用java端對數據庫表進行添加操作,出現以下錯誤:
ERROR AssertionFailure:22 - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
org.hibernate.AssertionFailure: null id in com.jeanswest.model.TAfteret entry (don't flush the Session after an exception occurs)請問下有人知道是怎么回事么?
下面hibernate映射文件:<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" ><hibernate-mapping package="com.jeanswest.model">
<class
name="TAfteret"
table="t_afteret"
>
<meta attribute="sync-DAO">false</meta>
<id
name="Id"
type="integer"
column="f_afteretid"
>
<generator class="native"/>
</id> <property
name="FIsaskchange"
column="f_isaskchange"
type="boolean"
not-null="false"
length="1"
/>
<property
name="FIsaskback"
column="f_isaskback"
type="boolean"
not-null="false"
length="1"
/>
<property
name="FDescription"
column="f_description"
type="string"
not-null="false"
/>
<property
name="FIsok"
column="f_isok"
type="boolean"
not-null="false"
length="1"
/>
<property
name="FIsokdescription"
column="f_isokdescription"
type="string"
not-null="false"
/>
<property
name="FIsafterchange"
column="f_isafterchange"
type="boolean"
not-null="false"
length="1"
/>
<property
name="FIsafterback"
column="f_isafterback"
type="boolean"
not-null="false"
length="1"
/>
<property
name="FAskchangetime"
column="f_askchangetime"
type="string"
not-null="false"
length="50"
/>
<property
name="FAskbacktime"
column="f_askbacktime"
type="string"
not-null="false"
length="10"
/>
<property
name="FAfterchangetime"
column="f_afterchangetime"
type="string"
not-null="false"
length="50"
/>
<property
name="FAfterbacktime"
column="f_afterbacktime"
type="string"
not-null="false"
length="50"
/>
<property
name="FIssend"
column="f_issend"
type="boolean"
not-null="false"
length="1"
/>
<property
name="FIssign"
column="f_issign"
type="boolean"
not-null="false"
length="1"
/>
<many-to-one
name="FOrderid"
column="f_orderid"
class="TOrder"
not-null="true"
>
</many-to-one>
<many-to-one
name="FGoodsid"
column="f_goodsid"
class="TGoods"
not-null="true"
>
</many-to-one>
</class>
</hibernate-mapping>

解决方案 »

  1.   

    可能是数据库里的问题
    将数据空中的f_afteretid字段设置成标识(identity(1,1))
      

  2.   

    hibernate的实现蛮复杂的,保存的时候hibernate会有2份记录,一份放缓存里,一份放自己的map里,你的save的时候,会产生这个类的代理类,保存的时候是id用的native,依靠数据库分配,执行了save方法后,数据库的id并没有分配, 调用了session的flush方法,会强制清理缓存,发出sql,然后将缓存中的indatabaseexists这个属性设置成true,而你再执行commit 的时候,然后它会来将map值和缓存中的值比较,但是你已经提交了,缓存中的已经没有了,所以它会告诉你这个数据是不安全的。不知道有没说清楚 ?看看代码或许更好
      

  3.   

    generator class="native"这个id应该没有生成,能否改成其他生成策略试试。
      

  4.   

    多謝各位的留言,昨天解決了問題,原來是自己數據庫AfterMarket表里的一個字段的長度設置少了。所以出現字符串截斷,然后就出現了這個異常don't flush the Session after an exception occurs"我個人的理解。