Hibernate的@OneToMany - 映射到多个连接表 [英] Hibernate @OneToMany - mapping to multiple join tables

查看:415
本文介绍了Hibernate的@OneToMany - 映射到多个连接表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的域模型:

Issue
- id
- List<Comment>

Entry
- id
- List<Comment>

Comment
-id
-comment

在我的设计,我试图创建两个连接表管理协会; issue_comments和entry_comments。我以为@OneToMany的问题和放大器;入境,但你如何映射多个连接表?使用Hibernate注释,这可怎么映射?

In my design, I was attempting to create two join tables to manage the associations; issue_comments, and entry_comments. I assumed @OneToMany on Issue & Entry, but how do you map the multiple join tables? Using hibernate annotations, how can this be mapped?

推荐答案

如果你可以改变你的域模型,看看通过的克莱图斯。你只有一个表进行更新,因此将提供更好的性能。

If you can change your domain model, take a look at answer given by cletus. You'll only have one table to update so it'll provide better performance.

如果你不能改变你的域模型,你可以映射你的注释通过连接表的集合:

If you cannot change your domain model, you can map your comment collections via join tables:

// ENTRY
@OneToMany
@JoinTable(
        name="ENTRY_COMMENTS",
        joinColumns = @JoinColumn( name="entry_id"),
        inverseJoinColumns = @JoinColumn( name="comment_id")
)
public List<Comment> getComments()

// ISSUE
@OneToMany
@JoinTable(
        name="ISSUE_COMMENTS",
        joinColumns = @JoinColumn( name="issue_id"),
        inverseJoinColumns = @JoinColumn( name="comment_id")
)
public List<Comment> getComments()

您的意见仍然会在这两个问题和条目相同的表;只联接表会有所不同。注意,这是一个单向关系。详情<一个href=\"http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#entity-mapping-association-collections\"相对=nofollow>这里

Your comments would still be in the same table for both issues and entries; only join tables will be different. Note that this is a uni-directional relationship. Details are here

这篇关于Hibernate的@OneToMany - 映射到多个连接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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