[JAVA] How to delete / update the list field of OneToMany

problem

--Even if you send the delete method to reposytory with the primary key, it seems that the record is not deleted, and dtl is searched by the hdr search for redisplay.

--Has the following entities --dtlEntity is child data (details) --hdrEntity is the parent --hdrEntity has dtlEntity in list field as below

HdrEntity


@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "tMitsumoriHdrEntity")
    @JsonIgnore
    private List<DtlEntity> dtlEntity;

--On the controller, delete dtlEntity with the delete method of the repository, but it seems to disappear on the screen, but the screen revives after another request, it seems that persistence is not done well

Premise

Countermeasures

1. Do not delete related Entity directly

--If it is related, if you delete it directly, the data will remain in the hdr and the deletion will return.

dtlRepository.delete(dtlEntity);

--If only, the dtlEntity still remains in the HdrEntity and will not disappear.

2. Set orphanRemoveal to true

--If you do not add the orphanRemoval = true attribute to oneToMany, even if you delete the related table, it will only be in memory and will not be persisted.

//Detail table
	@OneToMany(cascade = CascadeType.ALL, orphanRemoval=true, fetch = FetchType.EAGER, mappedBy = "tMitsumoriHdrEntity")
	@JsonIgnore
    private List<DtlEntity> dtlEntity;

3. Dealing with A collection with cascade = "all-delete-orphan" was no longer referenced error

--When you have an association entity in a list, you have to persist it by clearing the association with the clear method, adding and reinserting it.

--NG example

HdrEntity.setDtlEntity(saveDtlList);
HdRepository.save(HdrEntity);

--When making changes that change the number of items in the list --Assign the list after remove of the Entity you want to delete into the field, --When persisting with save in repository

A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance

--An error occurs

HdrEntity.getDtlEntity().clear();
HdrEntity.getDtlEntity().addAll(saveDtlList);
HdRepository.save(HdrEntity);

--First, clear () the field that contains the list of Entity, --Re-enter the changed list in that field again

reference

HibernateException - A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance
http://cristian.sulea.net/blog.php?p=2014-06-28-hibernate-exception-a-collection-with-cascade-all-delete-orphan-was-no-longer-referenced-by-the-owning-entity-instance


5.2. Database access (JPA edition) 5.2.2.11. Implementation of Entity deletion process https://terasolunaorg.github.io/guideline/public_review/ArchitectureInDetail/DataAccessJpa.html#id38


I searched about JPA http://juzow.hatenablog.com/entry/20121017/1350480972

Recommended Posts

How to delete / update the list field of OneToMany
How to sort the List of SelectItem
[Java] Delete the elements of List
How to add the delete function
How to display the select field of time_select every 30 minutes
[Rails] How to display the list of posts by category
How to delete the wrong migration file
How to delete the migration file NO FILE
How to determine the number of parallels
How to find the cause of the Ruby error
Add empty data to the top of the list
Customize how to divide the contents of Recyclerview
How to get today's day of the week
[For beginners] How to implement the delete function
How to delete the database when recreating the application
Output of how to use the slice method
How to display the result of form input
[Java] How to get the authority of the folder
[Java] How to get the URL of the transition source
How to write Scala from the perspective of Java
[Ruby] How to find the sum of each digit
How to install the root certificate of Centos7 (Cybertrust)
[Java] How to get the maximum value of HashMap
[Rails] How to change the column name of the table
[SwiftUI] How to specify the abbreviated position of Text
[Android] How to get the setting language of the terminal
[Rails] How to get the contents of strong parameters
How to judge the click of any area of the image
How to download the old version of Apache Tomcat
[Swift] How to get the document ID of Firebase
How to delete BOM (UTF-8)
How to update with activerecord-import
JPA's OneToMany delete becomes update
How to connect the strings in the List separated by commas
Delete all the contents of the list page [Ruby on Rails]
How to get the longest information from Twitter as of 12/12/2016
How to change the setting value of Springboot Hikari CP
[Ruby] How to retrieve the contents of a double hash
How to add elements without specifying the length of the array
Let's summarize how to extend the expiration date of Rails
How to solve the problems of Java's three Blocking Queues
How to mock some methods of the class under test
How to derive the last day of the month in Java
How to change the contents of the jar file without decompressing
How to check the extension and size of uploaded files
[jsoup] How to get the full amount of a document
How to check the database of apps deployed on Heroku
How to use the link_to method
[Swift] How to dynamically change the height of the toolbar on the keyboard
How to use the include? method
How to use the form_with method
[Rails] How to get the URL of the transition source and redirect
How to create a registration / update function where the table crosses
How to find the average angle
[Swift5] How to get an array and the complement of arrays
How to set the IP address and host name of CentOS8
[Java] How to update Java on Windows
How to use the wrapper class
How to display 0 on the left side of the standard input value
How to use setDefaultCloseOperation () of JFrame
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku