1. 程式人生 > >hibernate 級聯刪除報更新失敗的問題

hibernate 級聯刪除報更新失敗的問題

使用Hibernate框架,做級聯刪除時,出現一下報錯:

org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update

首先檢視級聯刪除物件的對映配置檔案, 是否有配置關聯關係;因為級聯對應關聯太多,這裡就不貼了。

再列印sql語句,查詢具體是在什麼地方出現錯誤:

Hibernate: 
    select
        purchaseor0_.id as id162_0_,
        purchaseor0_.allGoods_id as allGoods2_162_0_,
        purchaseor0_.purchaseOrder_id as purchase3_162_0_,
        purchaseor0_.planProcure_id as planProc4_162_0_,
        purchaseor0_.detectStatus as detectSt5_162_0_,
        purchaseor0_.fahuoStatus as fahuoSta6_162_0_,
        purchaseor0_.inStoreStatus as inStoreS7_162_0_,
        purchaseor0_.isToStock as isToStock162_0_,
        purchaseor0_.tuiHuanGoodsId as tuiHuanG9_162_0_,
        purchaseor0_.quantity as quantity162_0_,
        purchaseor0_.discount as discount162_0_,
        purchaseor0_.amount as amount162_0_,
        purchaseor0_.taxRate as taxRate162_0_,
        purchaseor0_.orderPrice as orderPrice162_0_,
        purchaseor0_.withTaxAmount as withTax15_162_0_,
        purchaseor0_.orderDate as orderDate162_0_,
        purchaseor0_.remark as remark162_0_,
        purchaseor0_.packingQuantity as packing18_162_0_,
        purchaseor0_.productionLotNumber as product19_162_0_,
        purchaseor0_.expirationDate as expirat20_162_0_ 
    from
        purchaseOrderItem purchaseor0_ 
    where
        purchaseor0_.id=?
... ...//查詢關聯物件的SQL
Hibernate: 
    update
        detection 
    set
        purchaseOrderItem_id=null 
    where
        purchaseOrderItem_id=?
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103) at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:254) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:169) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1001) at com.rhxy.dao_new.BaseSubTableDAO.delete(BaseSubTableDAO.java:299) ... ...//exception

注意紅色字型部分,問題就出在這裡,當前表與detection表存在一對多關係,是雙向關聯的:

這裡是修改前的對映檔案描述:

 <set name="detections">
            <key>
                <column name="purchaseOrderItem_id"/>
            </key>
            <one-to-many class="com.rhxy.bean_new.procurement.Detection"/>
        </set>
新增fetch="join":
 <set name="detections" fetch="join">
            <key>
                <column name="purchaseOrderItem_id"/>
            </key>
            <one-to-many class="com.rhxy.bean_new.procurement.Detection"/>
        </set>
至此,問題解決;還有需要注意的是detection標準的,關於當前物件是否允許為NULL? 如果不是,這裡也需要調整為允許為NULL

新增fetch="join" 是為了在查詢物件的時候,使用外連線來查詢關聯的detection物件;因為預設是使用延遲載入,所以當你不使用fetch

屬性的時候,查詢出來的關聯物件並不是一個真正的物件,只是一個代理;所以造成更新關聯物件失敗

關於 fetch屬性問題,請查詢百度,關於延遲載入的問題;