简体   繁体   中英

Java hibernate ManyToMany relation between more then two entities

I have problem, maybe not a problem, but still. I have two entities, User and Property. I want to make a third entity UserProperty. I need to map them by user_proeprties where they have some access roles stored. I user has many properties so User and properties have ManyToMany relation. I need to map them to UserProperty with ManyToMany relation. Is there any possibility?

Let me clearify:

I have entity of users who can have One to many Relation to Properties. A user can create many properties. Also UserProperty acts as following, a user who created a property can add other users as a housewife, gardener etc. This lets say access should be mapped through UserProperty. UserProeprty should be mapped to Users as manytomany and also to properties as manytomany, as a same user can work in multiple houses. Also I have Permission entity mapped to UserProperty as manytomany, becasue a user could have multiple roles in a house, which will be set to UserProperty and will assign their role. How can manage this? Users have Properties who have their own roles to a current property.

Here In this case you are choosing the wrong mapping. Instead of mapping User and User_properties in many to many relationships. You should create one table that have the primary key as composite primary key (user_id,user_property_id) that is called. You can map them in hibernate with @EmbeddedId annotation. and you can also have additional fields that you want.

Refrer this tutorial for implementation with examples:

https://vladmihalcea.com/the-best-way-to-map-a-composite-primary-key-with-jpa-and-hibernate/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM