Hibernate Many to Many on many to [英] Hibernate Many to Many on many to

查看:30
本文介绍了Hibernate Many to Many on many to的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用注释将现有数据库映射到 Hibernate.问题如下.

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

'organization' 和 'organization_roles' 有一个多对多的关系,在一个表'organization_has_roles' 使用两个主键作为连接表中的复合主键.

'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.

接下来是用户"表.用户可以在多个组织中拥有一个角色.这在名为users_has_organizations_and_role"的连接表中再次解决,该表具有user_id"、organization_id"和organization_role_id"的复合主键以及user_id"和organization_id"上的unique_index.

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'.

'users_has_organizations_and_role'引用了'organization_has_roles'复合主键(organization_id, organization_role_id)和users表(user_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?

我正在使用 Hibernate 4,虽然它可能无关紧要..

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

推荐答案

这种方法的主要问题是你有一个不可映射的 ORM 不匹配:你的数据库中有一个实体 (organization_has_roles)在 OO 中被翻译为一种关系.由于此关系不是实体,并且没有 @Id,因此无法将其引用到另一个实体中.

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.

一种解决方案是将数据库实体 organization_has_roles 映射到 JPA/Hibernate 实体,使用复合键或合成键(无论您喜欢什么).然后您可以在您的 User 模型中引用该实体.

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.

这篇关于Hibernate Many to Many on many to的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆