添加(外部)关系(标记)到现有的Hibernate实体 [英] Add (external) relationship (tagging) to existing Hibernate entities

查看:93
本文介绍了添加(外部)关系(标记)到现有的Hibernate实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要向现有的Hibernate实体添加一个新的多对多关系。
我不想触摸原始的Hibernate实体bean或其配置。
我添加的是一个标记功能,可以被视为外部贡献,而不是实体数据本身的一部分。

I need to add a new many-to-many relationship to an existing Hibernate entity. I do not want to touch the original Hibernate entity bean or its configuration. What I am adding is a "tagging" feature that can be viewed as an external contribution and not part of the entity's data itself.

我想要一个简单的连接表,只有两列,实体主键和标签id。

I want to have a simple join table with only two columns, the entity primary key, and the tag id.

我可以使用Hibernate来管理这个表,而不需要引入一个新的(人工的)实体类型包含单个标签映射?

Can I use Hibernate to manage this table without introducting a new (artificial) entity type that contains a single tag mapping?

或者我误导了,实际上希望拥有这个关系实体,以便我可以添加属性(如时间戳)稍后?

Or am I misguided and should actually want to have this "relationship-entity", so that I can add attributes (such as timestamps) later?

推荐答案

Hibernate背后的想法是能够从给定的起点遍历一个Java对象树。如果要定义从原始对象到标签对象的多对多关系,您仍然可以在标签对象上定义它,并且它将正确地允许您获取具有该标记的原始对象的列表。

The idea behind Hibernate is being able to traverse a Java object tree from a given starting point. If you want to define a many-to-many relationship from the original object to a tag object, you can still define it on the tag object, and it will properly allow you to get a list of the original objects that have that tag.

缺点是您无法查询原始对象的标签列表(需要注释才能反转关系,并返回一个访问器一组标签对象的列表)。然而,您将能够检索标记有给定标签的原始对象的列表。

The drawback is that you won't be able to query the original object for it's list of tags (that would require an annotation to reverse the relationship and an accessor that returned a list of Set of tag objects). You will however be able to retrieve a list of the original objects that marked with a given tag.

这里有一个例子...我们假设要标记的对象是一个帖子。如果是这样,可以在代码中添加多对多关系的代码,以便您可以查找具有特定标签的帖子列表:

Here's an example ... Let's assume that the object to be tagged is a Post. If so, here's the code to add a many-to-many relationship to the Tag, so that you can look up a list of Posts that have a specific Tag:

@ManyToMany
@JoinTable(
    name = "TAG-POST",
    joinColumns = {@JoinColumn(name = "TAG-ID")},
    inverseJoinColumns = {@JoinColumn(name = "POST-ID")}
)
private Set<Posts> posts = new HashSet<Post>();

通常,您还可以查看与帖子相关的所有标签,但您可以省略反向映射。如果您需要反向映射,则需要在Post对象中添加如下内容:

Normally, you'd also want to be able to look up all the Tags related to a Post, but you can leave out the reverse mapping. If you do need the reverse mapping, you'll need to add something like this to your Post object:

@ManyToMany(mappedBy = "tags")
private Set<Tag> tags = new HashSet<Tag>();

现在,您还可以查找与帖子相关的标签。

Now you can also look up the tags that are related to a Post.

重读你的帖子(和查看你的评论)后,我意识到你也有兴趣跳过创建一个标签实体。如果没有标签名称,你可以想象只能使用你所描述的表,但是你需要改变你的心态。您真正描述的是Post与其标签条目之间的一对多关系。在这种情况下,您需要将帖子映射到一系列标记记录,其中包含两列:POST-ID和TAG-NAME。不改变原始对象,您仍然可以查询表格,查看具有特定TAG-NAME的帖子列表或与特定帖子相关的TAG-NAME行列表。

After rereading your post (and viewing your comment), I realize that you're also interested in skipping the creation of a Tag entity. If there is nothing but tag name, you could conceivably only use the table you've described, but you need to shift your mindset a bit. What you're really describing is a one-to-many relationship between the Post and its Tag entries. In this case, you'll need to map a post to a series of tag records, that have two columns ... a POST-ID and a TAG-NAME. Without altering the original object, you can still query the table for a list of Posts with a specific TAG-NAME or for a list of TAG-NAME rows that are related to a specific Post.

请注意,这实际上不会消除实体...您不会有Tag实体,但是多对多查找表必须以多对一的关系创建,这使它成为一个实体本身。然而,这种方法使用一个表。

Note that this doesn't actually eliminate an entity ... You won't have the Tag entity, but the many-to-many lookup table will have to be created as a many-to-one relationship, which makes it an entity itself. This approach does however use one less table.

这篇关于添加(外部)关系(标记)到现有的Hibernate实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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