`

解决hibernate 删除异常:deleted object would be re-saved by cascade (remove deleted obj

阅读更多

解决hibernate 删除异常:deleted object would be re-saved by cascade (remove deleted object from2009-08-19 22:16我遇到到是第三种

解决hibernate 删除异常: deleted object would be re-saved by cascade (remove deleted object from associations) 收藏
在hibernate 删除关联时会出现eleted object would be re-saved by cascade (remove deleted object from associations)的异常,结合别人的和自己的经验通常有三种解决的方案:

方法1 删除Set方的cascade:
方法2 解决关联关系后,再删除

    onside.getManys().remove(thisMany);   //在所关联的一方的set中移走当前要删除的对象
    thisMany.setOne(null);                          //设置所对应的一方为空,解除它们之间的关系
    manyDao.delete(thisMany);
方法3 在many-to-one方增加cascade 但值不能是none
 


主要讲讲要点(以OneToMany为例):

1.如果Many端存在于One端的集合中,直接删除Many,就会抛出"deleted object would be re-saved by cascade"的异常,解决办法是在One端的集合中先删除Many端(先把他们之间的联系割断),然后再在Hibernate中删除Many端。
但在JDBC下直接删除Many端是可以的。

2.如果直接删除One端:
   A.如果One端中没有设置Cascade为delete或者all,就会抛出“Cannot delete or update a parent row: a foreign key constraint fails”的异常。在JDBC下也是不行的(在建表时设置 ON DELETE CASCADE除外),因为Hibernate只是把JDBC下的异常封装而已。
   B.如果One端中设置Cascade为delete或者all,删除的时候就会把所有Many端都删除。

分享到:
评论

相关推荐

    EurekaLog_7.5.0.0_Enterprise

    7)....Fixed: Attaching .PAS files instead of .OBJ in C++ Builder 2006+ Pro/Trial EurekaLog 7.2 Hotfix 1 (7.2.1.0), 3-April-2015 1)....Fixed: Wrong float-str convertion when ThousandSeparator is '.' ...

    android扫雷源码

    安卓 扫雷源码 添加重新开始按钮支持 Android studio =========================...(This import summary is for your information only, and can be deleted after import once you are satisfied with the results.)

    触发器实现sql记录

    在许多应用中,需要保存某个数据表的历史记录,以供以后查询,在 SQL Server 2000 中,可以利用触发器轻松实现历史记录功能。... delete 被删除的记录 [不可用] ---------------------------------------------------

    MySQL 8.0.13设置日期为0000-00-00 00:00:00时出现的问题解决

    刚开始学习数据库操作,今天存数据时发现,保存的时候报错(Error 1292: Incorrect datetime value: '0000-00-00' for column 'deleted_at' at row 1) ,之后就搜索了下原因,是因为MySQL存日期时不允许出现这种格式...

    delphi编译错误.txt

    FOR or WHILE loop executes zero times - deleted FOR或WHILE循环执行零次-删除 FOR-Loop variable ''''<name>'''' cannot be passed as var parameter FOR循环变量不能作为参数传递 FOR-Loop variable ''''<Name>'...

    sql触发器实例详细讲解

    定义: 何为触发器?在SQL Server里面也就是对某一个表的一定的操作,触发某种条件,从而... 一个Update 的过程可以看作为:生成新的记录到Inserted表,复制旧的记录到Deleted表,然后删除Student记录并写入新纪录。

    Exploring the location of object deleted by seam-carving

    Exploring the location of object deleted by seam-carving

    React 脚手架的安装方法

    react 脚手架的安装方法 安装的方法都与个人的使用习惯相关,以下就介绍两种使用较多的 node环境下 方法一: 全局安装 npm install create-react-app -g 还可以查看版本号 create-react-app -V ...

    edjpgcom|图片中插入一句话

    should still have your original file (unless you deleted it!). You might have to clean up a .nnn file but that's it. It's been tested on Win2k, NT 4.0 SP5, Win95a, Win98, and Win98SE. Limitations:...

    Zteam原始代码

    1> Deleted SEASON5DOT2_JPN, all changes undefined 1> Changed XOR32 keys 1> More test changes in DSProtocol, user and etc 1> [JewelMixSystem.cpp] Fixed item delete in ::MixJewel 1> [JewelMixSystem.cpp]...

    RemoveSymantecMacFiles.command

    that would be deleted. No note is added if a file is not supposed to be removed by Symantec Uninstaller. -f Do not show files as they are removed. If -f is not specified, file names are shown as ...

    alfresco-deleted-content-store-cleaner:Alfresco删除的Content Store Cleaner作业

    Alfresco删除的内容商店清洁程序 该加载项提供了一项工作,以删除“已删除的内容存储”中的废弃文件。 从Alfresco 5.2(201707 GA)开始,已提供开箱即用的Trashcan Cleaner作业,尽管默认情况下是禁用的。 这项...

    谷歌地图 delphi 封装库 2013 0.1.9 全面支持google maps api

    - Improvement: TLinkedComponent => added FObject (TObject) property. - Improvement: TLinkedComponent => added protected methods (SetProperty) to generalize change into properties to avoid duplicate ...

    应用Dephi 开发佳能照相机API

    {****************************************************************************** * * ... Gets an indexed child object of the designated object. Parameters: In: inRef - The reference...

    tg-notify-deleted-messages:电报客户端以保存并通知已删除的消息

    tg-notify-deleted-messages允许您跟踪被对话者删除的消息。 删除后,它们将与有关发件人的元数据一起存储在“已保存的消息”中。 它还支持附件,而无需将其存储在磁盘上。 组态 前往 。 选择“ API开发工具”并...

    修复Linux异常自动重启问题

    这几天训网络最困扰我的问题就是服务器...EXT4-fs error (device sda1): ext4_lookup:1593: inode #6004222: comm trash-expunge: deleted inode referenced: 6004301 类似这样的错误,看样子是sda1磁盘文件系统的问题

    sgcWebSockets_source_4.1.0

    [+] : New Property "NotifyDeletes" in Dataset Server Protocol, if enabled (by default) broadcast deleted record to all clients. [+] : New Method "BroadcastRecord" in Dataset Server Protocol, sends ...

    SQLite-Deleted-Records-Parser, 在SQLite数据库中,用于恢复已经删除项目的脚本.zip

    SQLite-Deleted-Records-Parser, 在SQLite数据库中,用于恢复已经删除项目的脚本 SQLite解析器在SQLite数据库中恢复删除条目并将输出放入TSV文件或者文本文件( R ) 中的脚本sqlparse.py的###Usagesqlparse.py -f/...

    danbooru-recontaminant

    当前,除非指定了status: term,否则在Danbooru上的帖子搜索默认情况下会排除已删除的帖子。 Recontaminant是一种应用了相反行为的网络扩展,因此默认情况下会包含已删除的帖子。 只要在表达式中不存在其他任何...

    wechat-deleted-friends-master

    通过此代码,查看微信被删好友

Global site tag (gtag.js) - Google Analytics