Hibernatelush() 和 Cascade.PERSIST

发布于 2025-01-01 04:21:40 字数 1256 浏览 2 评论 0原文

我从一段时间以来就一直在研究 Hibernate,直到昨天我才真正关注过lush() 方法。

据我了解,提交事务时会自动调用flush()。

我正在开发一个示例图书馆管理系统。 AuthorsBooks的关系是ManyToMany

在Author.java中

@Fetch(FetchMode.SELECT)
@ManyToMany(mappedBy="authors", cascade={CascadeType.PERSIST, CascadeType.MERGE})
private Set<Book> books = new HashSet<Book>();

,在Book.java中

@ManyToMany
@Cascade({org.hibernate.annotations.CascadeType.PERSIST})
@JoinTable(
       name="BookAuthor",
       joinColumns={@JoinColumn(name="bookId", referencedColumnName="id")},
       inverseJoinColumns={@JoinColumn(name="authorId", referencedColumnName="id")}
      )
private Set<Author> authors = new HashSet<Author>();

有一个映射表BookAuthor在数据库中。

这就是我正在做的事情。

  1. 将 FlushMode 设置为 MANUAL
  2. 从数据库获取一本书
  3. 创建一个新作者。 (暂时)
  4. 将作者添加到 Book 实例中的 authors 集中。
  5. 开始事务
  6. 更新图书实例
  7. 提交事务。

与我的预期相反,我注意到更新这本书没有传播并保留了暂时的 Author 实例。 Author 实例仍然是暂时的。

现在,如果我将FlushMode 设置为AUTO(默认),则不仅会提交临时作者,还会在 BookAuthor 表中创建一个关系。

我很惊讶地知道flush() 可以发挥如此重要的作用。

我做错了什么吗?

问候,

沙杜尔。

I have been working on Hibernate since sometime and I never really paid attention to the flush() method until yesterday.

I understand that flush() is invoked automatically when the transaction is committed.

I am working on a sample Library Management System. The relation between Authors and Books is ManyToMany

In Author.java

@Fetch(FetchMode.SELECT)
@ManyToMany(mappedBy="authors", cascade={CascadeType.PERSIST, CascadeType.MERGE})
private Set<Book> books = new HashSet<Book>();

In Book.java

@ManyToMany
@Cascade({org.hibernate.annotations.CascadeType.PERSIST})
@JoinTable(
       name="BookAuthor",
       joinColumns={@JoinColumn(name="bookId", referencedColumnName="id")},
       inverseJoinColumns={@JoinColumn(name="authorId", referencedColumnName="id")}
      )
private Set<Author> authors = new HashSet<Author>();

There is a mapping table BookAuthor in the database.

This is what I am doing.

  1. Set FlushMode to MANUAL
  2. Get a Book from the database
  3. Create a new Author. (Transient)
  4. Add author to the authors Set in the Book instance.
  5. Begin Transaction
  6. Update book instance
  7. Commit transaction.

Contrary to my expectation, I notice that updating the Book has NOT propagated and persisted the transient Author instance. The Author instance is still transient.

Now, if I set the FlushMode to AUTO(default), not only is the transient Author committed but a relation is also created in the BookAuthor table.

I am surprised to know that flush() has such an important role to play.

Am I doing something wrong?

Regards,

Shardul.

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

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

发布评论

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

评论(1

别靠近我心 2025-01-08 04:21:40

flush 是将更改写入数据库的方法。没有冲洗,就什么也写不出来。显然,如果您将刷新模式设置为手动并且从不调用刷新,则不会将任何内容写入数据库。

flush is the method that writes changes to the database. Without flushing, nothing is written. So obviously, if you set flush mode to manual and never call flush, nothing will be written to the database.

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