Hibernate:deleted object would be re-saved by cascade (remov
Hibernate多表关联的时候的异常:deleted object would be re-saved by cascade (remove deleted object from associations): []。 【产生原因】 表之间的一对多(多对一)关联,两种情况:双表一对多多对一,单表自关联一对多多对一。表现为:删除“多”的
Hibernate多表关联的时候的异常:deleted object would be re-saved by cascade (remove deleted object from associations): []。
【产生原因】表之间的一对多(多对一)关联,两种情况:双表一对多多对一,单表自关联一对多多对一。表现为:删除“多”的一方的条目的时候出现这个异常。
【具体环境背景】楼主是在单表自关联(双向一对多多对一)出现的,建表细节:
<code class=" hljs java"><span class="hljs-annotation">@Entity</span> <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Department</span> {</span> <span class="hljs-keyword">private</span> Long id; <span class="hljs-keyword">private</span> Set<User> users = <span class="hljs-keyword">new</span> HashSet<User>(); <span class="hljs-keyword">private</span> Department parent; <span class="hljs-keyword">private</span> Set<Department> children = <span class="hljs-keyword">new</span> HashSet<Department>(); <span class="hljs-keyword">private</span> String name; <span class="hljs-keyword">private</span> String description; <span class="hljs-annotation">@Id</span> <span class="hljs-annotation">@GeneratedValue</span> <span class="hljs-keyword">public</span> Long <span class="hljs-title">getId</span>() { <span class="hljs-keyword">return</span> id; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setId</span>(Long id) { <span class="hljs-keyword">this</span>.id = id; } <span class="hljs-annotation">@ManyToOne</span>(cascade=CascadeType.MERGE) <span class="hljs-annotation">@JoinColumn</span>(name=<span class="hljs-string">"parent_id"</span>) <span class="hljs-keyword">public</span> Department <span class="hljs-title">getParent</span>() { <span class="hljs-keyword">return</span> parent; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setParent</span>(Department parent) { <span class="hljs-keyword">this</span>.parent = parent; } <span class="hljs-annotation">@OneToMany</span>(mappedBy=<span class="hljs-string">"parent"</span>,cascade=CascadeType.ALL,fetch=FetchType.EAGER) <span class="hljs-keyword">public</span> Set<Department> <span class="hljs-title">getChildren</span>() { <span class="hljs-keyword">return</span> children; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setChildren</span>(Set<Department> children) { <span class="hljs-keyword">this</span>.children = children; } }</code>
可见是Department部门表的自关联,每个部门都有一个上级部门和一个下级部门。我们理想状态的删除是:删除一个部门,1、它所有的下级部门级联删除,2、所有的上级部门不发生任何变化。
【解决方法!!!】由异常信息得知:deleted object would be re-saved by cascade (remove deleted object from associations)—->删除掉的对象将会被级联第二次保存(从绑定的联系上移除已经被删除的对象)。这说明我们已经做到了删除但是却由于级联(cascade)的原因,删除的对象又被利用了等等……,所以要做的是解决办法是:把@OneToMany里的cascade=CascadeType.ALL改成cascade=CascadeType.REMOVE。其他无关。
**【总结】**1、善于阅读思考异常内容。
2、cascade=CascadeType.ALL慎用,无论是在Many的一方还是One的一方。
3、当然,扎实基础是关键。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Integrating Hibernate in SpringBoot Project Preface Hibernate is a popular ORM (Object Relational Mapping) framework that can map Java objects to database tables to facilitate persistence operations. In the SpringBoot project, integrating Hibernate can help us perform database operations more easily. This article will introduce how to integrate Hibernate in the SpringBoot project and provide corresponding examples. 1.Introduce dependenciesIntroduce the following dependencies in the pom.xml file: org.springframework.bootspring-boot-starter-data-jpam

Java is an object-oriented programming language that is widely used in the field of software development. Hibernate is a popular Java persistence framework that provides a simple and efficient way to manage the persistence of Java objects. However, Hibernate errors are often encountered during the development process, and these errors may cause the program to terminate abnormally or become unstable. How to handle and avoid Hibernate errors has become a skill that Java developers must master. This article will introduce some common Hib

The differences between hibernate and mybatis: 1. Implementation method; 2. Performance; 3. Comparison of object management; 4. Caching mechanism. Detailed introduction: 1. Implementation method, Hibernate is a complete object/relational mapping solution that maps objects to database tables, while MyBatis requires developers to manually write SQL statements and ResultMap; 2. Performance, Hibernate is possible in terms of development speed Faster than MyBatis because Hibernate simplifies the DAO layer and so on.

Hibernate's one-to-many and many-to-many Hibernate is an excellent ORM framework that simplifies data access between Java applications and relational databases. In Hibernate, we can use one-to-many and many-to-many relationships to handle complex data models. Hibernate's one-to-many In Hibernate, a one-to-many relationship means that one entity class corresponds to multiple other entity classes. For example, an order can correspond to multiple order items (OrderItem), and a user (User) can correspond to multiple orders (Order). To implement a one-to-many relationship in Hibernate, you need to define a collection attribute in the entity class to store

Java uses the getClass() function of the Object class to obtain the runtime class of the object. In Java, each object has a class, which defines the properties and methods of the object. We can use the getClass() function to get the runtime class of an object. The getClass() function is a member function of the Object class, so all Java objects can call this function. This article will introduce how to use the getClass() function and give some code examples. use get

1. Introduction to the Object class Object is a class provided by Java by default. Except for the Object class, all classes in Java have inheritance relationships. By default, it will inherit the Object parent class. That is, objects of all classes can be received using the reference of Object. Example: Use Object to receive objects of all classes classPerson{}classStudent{}publicclassTest{publicstaticvoidmain(String[]args){function(newPerson());function(newStudent());}public

Object to byte and byte to Object Today we will realize how to convert from Object to byte and how to convert from byte to Object. First, define a class student: packagecom.byteToObject;importjava.io.Serializable;publicclassstudentimplementsSerializable{privateintsid;privateStringname;publicintgetSid(){returnsid;}publicvoidsetSid(in

Hibernate is an open source ORM framework that binds the data mapping between relational databases and Java programs to each other, making it easier for developers to access data in the database. Using the Hibernate framework can greatly reduce the work of writing SQL statements and improve the development efficiency and reusability of applications. Let's introduce the Hibernate framework from the following aspects. 1. Advantages of the Hibernate framework: object-relational mapping, hiding database access details, making development
