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属性问题,请查询百度,关于延迟加载的问题;



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值