Hibernate 双向多对多映射建议!

发布于 2024-08-30 23:31:02 字数 914 浏览 8 评论 0原文

如果有人能帮助我,我很受伤。我正在尝试找出要搜索的内容(或任何其他想法!!)基本上我在用户实体和俱乐部实体之间有一个双向多对多映射(通过名为 userClubs 的联接表)我现在想包括一列在 userClubs 中代表角色,这样当我调用 user.getClubs() 时,我还可以计算出他们拥有什么级别的访问权限。有没有一种聪明的方法可以使用休眠来做到这一点,或者我是否需要重新考虑数据库结构?感谢您的任何帮助(或只是阅读到目前为止!!)

user.hbm.xml 看起来有点像

<set name="clubs" table="userClubs" cascade="save-update">
         <key column="user_ID"/>
         <many-to-many column="activity_ID"
           class="com.ActivityGB.client.domain.Activity"/>
</set>

Activity.hbm.xml 部分

<set name="members" inverse="true" table="userClubs" cascade="save-update">
        <key column="activity_ID"/>
        <many-to-many column="user_ID"
            class="com.ActivityGB.client.domain.User"/>
</set>

当前的 userClubs 表包含字段 编号 |用户 ID | activity_ID

我想包含在其中的 编号 |用户 ID |活动ID |角色

并能够访问双方的角色...

i woundered if anyone might be able to help me out. I am trying to work out what to google for (or any other ideas!!) basically i have a bidirectional many to many mapping between a user entity and a club entity (via a join table called userClubs) I now want to include a column in userClubs that represents the role so that when i call user.getClubs() I can also work out what level access they have. Is there a clever way to do this using hibernate or do i need to rethink the database structure? Thank you for any help (or just for reading this far!!)

the user.hbm.xml looks a bit like

<set name="clubs" table="userClubs" cascade="save-update">
         <key column="user_ID"/>
         <many-to-many column="activity_ID"
           class="com.ActivityGB.client.domain.Activity"/>
</set>

the activity.hbm.xml part

<set name="members" inverse="true" table="userClubs" cascade="save-update">
        <key column="activity_ID"/>
        <many-to-many column="user_ID"
            class="com.ActivityGB.client.domain.User"/>
</set>

The current userClubs table contains the fields
id | user_ID | activity_ID

I would like to include in there
id | user_ID | activity_ID | role

and be able to access the role on both sides...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飞烟轻若梦 2024-09-06 23:31:02

最简单的方法是从连接表中创建一个实体。

您可以使用俱乐部和用户实体的两个外键作为 id(使用),但我建议添加一个技术 id 列,并只对外键设置唯一约束(否则,保存俱乐部或用户时的级联将不起作用,更多信息如下:http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-compositeid)。

如果将新实体命名为 UserClub,则需要从 User 到 UserClub、从 Club 到 UserClub 的一对多,以及从 UserClub 到 Club 和从 UserClub 到 User 的多对一。

您将对 java 代码进行一些重构,但不必更改数据库(除了添加 id 和 role 列)。

The easiest way would be to create an entity out of the join table.

you could use the two foreign keys to the club and user entities as the id (using <composite-id>), but i would advise adding a technical id column, and just set a unique constraint on the foreign keys (otherwise, the cascade when you save Club or User won't work. more info here : http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-compositeid).

If you name your new entity UserClub, you would need a one-to-many from User to UserClub and from Club to UserClub, and many-to-one from UserClub to Club and from UserClub to User.

You will have some refactoring to do on the java code but you won't have to change the db (apart from adding the id and role columns).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文