Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What if the many in onetomany cannot be deleted in jpa

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report