Hibernate Many to Many on many to

泪湿孤枕 提交于 2019-12-08 07:41:11

问题


I´m trying to map an existing database to Hibernate using annotations. The problem is as follows.

'organization' and 'organization_roles' has a many to many relationship which is resolved in a table 'organization_has_roles' using the two primary keys as a composite primary key in the join table.

Next there is a 'users' table. Users can have a single role in multiple organizations. This is again resolved in a join table named 'users_has_organizations_and_role' which has a composite primary key of the 'user_id', 'organization_id' and 'organization_role_id' and a unique_index on the 'user_id' and 'organization_id'.

The 'users_has_organizations_and_role' references the 'organization_has_roles' composite primary key(organization_id, organization_role_id) and the users table (user_id)

I have not been able to find examples on this matter that worked.

Is it possible to map this. Which Annotations should I use and how?

I´m using Hibernate 4, although it might not matter..


回答1:


The main problem with this approach is that you have an unmappable ORM mismatch: you have an entity in your database (organization_has_roles) which is translated as a relationship in OO. As this relationship is not an entity, and has no @Id, it cannot be referenced into yet another entity.

A solution is to map the database entity organization_has_roles into a JPA/Hibernate entity, with either a composite key, or with a synthetic key (whatever you prefer). You can then refer to this entity in your User model.



来源:https://stackoverflow.com/questions/11409014/hibernate-many-to-many-on-many-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!