Get the App
SLTechnology News&Howtos  ›  Development  › 

What if the many in onetomany cannot be deleted in jpa

Shulou Source: shulou.com Published: 2022-06-02 07:58:05 09月14日 Update

This article mainly introduces "what to do if you can't delete many in onetomany in jpa". In daily operation, I believe many people have doubts about how to delete many in onetomany in jpa. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubt that "what to do if you can't delete many in onetomany in jpa"! Next, please follow the editor to study!

Jpa cannot delete the many problem description in onetomany

Today, I encountered a problem when I was working on a project. I set up two entities, order and orderDetail, which are onetomany relationships. When I wanted to delete an orderDetail between them, I found that it could not be deleted but the program did not make an error.

The entity model is shown in the following figure:

In order:

In orderDetail:

Solution idea

1. First of all, I thought that it is not possible to delete orderDetail directly, whether it can be cascaded and deleted directly through the association of update order, so I wrote the code.

It turns out that it's possible to delete orderDetail, but it's not what I want, and it doesn't feel like the best solution.

two。 After the successful implementation of the previous method, I suddenly wondered whether it was the reason for the establishment of the association relationship, and if I disconnected the association relationship first, whether I could delete the orderDetail successfully:

It turns out that the deletion was successful.

Make a brief summary

Although I have achieved the function I want, the specific logic of jpa deletion still needs to be studied in depth. Self-feeling is because order is the maintainer of the relationship. If you want to delete the many side, you must first disconnect the relationship and delete it.

Jpa @ OneToMany cannot delete collection data

When using jpa, if there is a @ OneToMany relationship. Want to delete individual data from many. Sometimes I can't delete it. The jap relationship is as follows: one user corresponds to multiple roles. And CascadeType.ALL in the user entity

/ / user entity @ OneToMany (cascade = CascadeType.ALL,fetch= FetchType.LAZY,mappedBy = "user") private Set roles = new HashSet (); / / role entity @ ManyToOne (fetch= FetchType.LAZY) @ JoinColumn (name= "user_id") private User user

If you want to delete role at this time. There are two things that must be done: 1. Delete role. 2. Remove role from the collection roles. As follows:

RoleRepo.delete (role); / / here the delete operation requires both delete and remove from the collection to take effect user.getRoles () .remove (role); userService.update (user)

If you don't do this. It won't work.

At this point, the study on "what to do if the many in jpa cannot be deleted from onetomany" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Entities what to do problems learning success methods associations feelings data more results help no practical next between two things code function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MySQL Microsoft Apple Shulou Tech Info