Hibernate的多对多的很多人 [英] Hibernate Many to Many on many to

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

问题描述

我正尝试映射现有的数据库使用注释休眠。
问题是如下

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

组织和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)和用户表(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?

I'm使用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的实体,无论是复合键,或用合成关键(不管你preFER )。然后,您可以参考这个实体在用户模式。

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的多对多的很多人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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