In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "two-way control example analysis of ManyToMany and OneToMany in Jpa". In daily operation, I believe that many people have doubts about the two-way control example analysis of ManyToMany and OneToMany in Jpa. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "two-way control example analysis of ManyToMany and OneToMany in Jpa". Next, please follow the editor to study!
Bi-directional Control of Jpa ManyToMany and OneToMany
Let's use the relationship between RoleAccount (user ManyToMany account) and RoleDomain (user OneToMany permission domain) in permission management as an example.
1 、 ManyToMany
Role table
Account table
Add JoinColumns and inverseJoinColumns to the corresponding attributes of the two tables and exchange them with each other.
JoinColumn specifies the column name of this table in the intermediate table.
InverseJoinColumns specifies the column name of the accused party.
If you add these two attributes to both classes and exchange values, you can achieve two-way control, that is, if either party deletes, the data of the corresponding intermediate table will be automatically deleted.
2. OneToMany and ManyToOne
Role table
Domain table
Cascade is used to specify cascading operations. The value of cascade can only be selected from CascadeType.PERSIST (cascading new), CascadeType.REMOVE (cascading delete), CascadeType.REFRESH (cascading refresh), and CascadeType.MERGE (cascading update). Another option is to use CascadeType.ALL, which means to select all four items.
If you do not specify Cascade, the default is empty, then when you delete Role, only the value of the column role in the Domain table will be deleted, but the record will not be deleted.
JoinColumn needs to be specified, and if it is not specified, an additional column will be added to the Domain table, which is the same as Domain's id. After experiments, unspecified will also cause cascade search failure, the reason is unknown.
The problem of ManyToMany relation in JPA
When configuring JPA, the following error was reported in a many-to-many relationship:
Org.hibernate.AnnotationException: Illegal use of mappedBy on both sides of the relationship: com.csair.gme.core.domain.ComponentType.componentPropertys
Solution.
You can't use mappedBy on both sides, only one side, and the side that uses mappedBy is the slave table, and the other side is the master table. Variable name in the mappedBy=' main table'
Problem solved!
At this point, the study of "two-way control example analysis of ManyToMany and OneToMany in Jpa" is over. I hope to be able to 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.