寻求 Hibernate 中多对多属性持久性的帮助

发布于 2024-09-18 12:38:21 字数 983 浏览 2 评论 0原文

这里我有一个关于持久化多对多属性的问题。

示例:

class Movie {
    /**
     * @hibernate.bag cascade="save-update" table="movie_genre"
     * @hibernate.collection-key column="ITEM_ID"
     * @hibernate.collection-many-to-many column="GENRE_ID" class="Genre"
     * @hibernate.collection-cache usage="read-write"
     */
    public List<Genre> getGenres() {
        return genres;
    }
...  
}

class Genre {
}

假设 db 中有两部电影:
电影 A 类型为 X1 和X2
电影 B 类型为 Y1 和Y2

现在我将使用以下代码将类型从电影 A 复制到电影 B:

Movie b = findById('B');
Movie a = findById('A');
a.setGenres(b.getGenres());
session.saveOrUpdate(a);
session.flush();

现在,原始电影 B 和电影 B 的关系是:流派被删除。但是,如果我参考以下代码:

a.setGenres(new ArrayList(b.getGenres()));

那么,电影 B &保留流派关系。

在第一个代码片段中:hibernate会触发关系表movie_genre上的两条删除sql,分别删除Movie B和Movie B。关系;
在第二个中:hibernate将触发删除sql以仅删除电影A。

这是 Hibernate 持久化关系对象的机制吗?你能给我一些提示吗?

Here I have a question about persist many-to-many properties.

Example:

class Movie {
    /**
     * @hibernate.bag cascade="save-update" table="movie_genre"
     * @hibernate.collection-key column="ITEM_ID"
     * @hibernate.collection-many-to-many column="GENRE_ID" class="Genre"
     * @hibernate.collection-cache usage="read-write"
     */
    public List<Genre> getGenres() {
        return genres;
    }
...  
}

class Genre {
}

Supposed, there are two movies in db:
Movie A with Genre X1 & X2
Movie B with Genre Y1 & Y2

Now I am going to copy genres from Movie A to Movie B with the following code:

Movie b = findById('B');
Movie a = findById('A');
a.setGenres(b.getGenres());
session.saveOrUpdate(a);
session.flush();

Now, the relationship of original Movie B & genres are removed. But, if I refer to the following code:

a.setGenres(new ArrayList(b.getGenres()));

Then, the Movie B & genres relationship are kept.

In the first code snippet: hibernate will trigger two delete sql on relationship table movie_genre, which removing Movie B & A relationships;
In the second one: hibernate will trigger delete sql to remove Movie A only.

Is this the mechanism for Hibernate to persist relationship objects? Could you please give me some hints?

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

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

发布评论

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

评论(1

哥,最终变帅啦 2024-09-25 12:38:21

Hibernates为你创建的流派列表不是一个简单的java.util.LinkedList,它连接到Session并支持延迟获取、最小放置等。
在这种情况下,它携带其父对象的知识,当您移动它时,它会对关联表进行不需要的更新。

相反,您应该将实际的集合设为最终的,避免使用 setGenre() ,而应该引入“更智能”的变异器,例如 addToGenre(Genre...genres)、replaceGenres(List newGenres)。

希望这有帮助!

The Genres list which Hibernates creates for you is not a simple java.util.LinkedList, it is connected to the Session and supports lazy-fetching, minimal puts and whatnot.
In this case, it carries knowledge of its parent object and when you move it, it makes the undesired updates to the association table.

Instead, you should make the actual collection final, avoid setGenre() and instead of it, you should introduce "smarter" mutators, like addToGenre(Genre... genre), replaceGenres(List newGenres).

Hope this helps!

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