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属性问题,请查询百度,关于延迟加载的问题;
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 级联删除报更新失败的问题(org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update)
- Hibernate 级联删除失败问题
- Hibernate级联更新有新加入数据,更新失败
- hibernate annotation+spring 级联更新问题解决
- hibernate级联保存更新、删除
- Hibernate中单向一对多删除级联数据问题
- Hibernate一对多关联映射的配置及其级联删除问题
- hibernate 级联删除的问题
- 删除Maven更新失败后遗留的.lastUpdate文件问题
- Hibernate 一对多级联更新(更新,删除多方数据)的错误解决
- 关于hibernate删除对象不级联的问题
- mysql 级联更新删除问题
- Hibernate - 多对多级联删除的问题
- hibernate - 解决表结构外键限定为非空,级联保存和级联删除抛异常的问题
- Hibernate中hbm文件Set集合顺序对级联插入和删除的影响问题
- hibernate级联更新外键 需要注意的问题
- Hibernate单向多对一级联删除引发的问题
- hibernate中关于级联删除的问题
- Hibernate多对多级联删除的问题
- Hibernate注解中的manytomany级联与不级联删除问题的解决