您的位置:首页 > 其它

hibernate 级联删除报更新失败的问题

2017-10-21 11:40 555 查看
使用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
4000
.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属性问题,请查询百度,关于延迟加载的问题;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Hibernate