且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

添加(外部)关系(标记)到现有的Hibernate实体

更新时间:2023-12-05 21:48:40

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.