Hibernate刷新不更新数据库

发布于 2024-07-11 16:41:49 字数 355 浏览 7 评论 0原文

我正在使用 hibernate 来存储来自 Web 服务的一组对象。

当收到每个对象时,我使用休眠保存它们。

接收对象被包装在事务中,并且在接收到最终对象后所有对象都出现在数据库中。

我现在尝试让每个对象在保存时出现在数据库中。 我试图通过

getHibernateTemplate().saveOrUpdate( foo );

getHibernateTemplate().flush();
getHibernateTemplate().clear();

我的理解来实现这一点,这应该删除休眠值的缓存并将值写入数据库。

有什么学习或方向吗?

I'm using hibernate to store a set of objects from a web service.

As the object are received each I am saving them using hibernate.

Receiving the objects is wrapped in a transaction and all the objects appear in the database after the final object is received.

I am now trying have each object appear in the database when saved. I've tried to achieve this with

getHibernateTemplate().saveOrUpdate( foo );

getHibernateTemplate().flush();
getHibernateTemplate().clear();

My understanding is this should remove the values hibernate's cache and write the values to the database.

Any learning or directions?

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

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

发布评论

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

评论(2

万水千山粽是情ミ 2024-07-18 16:41:49

感谢布莱恩的帮助。 问题原来是包装保存调用的另一个类中的 for 循环。

解决方案是删除 for 循环并将其替换为 iterator

Hibernate 在整个 for 循环中保持相同的事务。 使用迭代器,Hibernate 似乎启动了一个新事务,因此执行对数据库的提交,然后在开始下一个事务之前执行刷新。

Thanks for the help Brian. The problems turned out to be a for loop in another class wrapping the save call.

The solution was to remove the for loop and replace it with an iterator.

Hibernate was keeping the same transaction for the entire for loop. Using the iterator, Hibernate seems to start a new transaction and hence performs the commit to the database and then a flush before beginning the next transaction.

余生再见 2024-07-18 16:41:49

如果您仍在事务中,则只有打开事务的会话或连接才能看到记录。 在某些数据库中,如果执行脏读/未提交读,您应该从另一个会话中看到它们。 我会尝试在刷新后使用相同的 Hibernate 会话运行选择,以验证它确实在数据库中。 只是不要通过主键查询,否则您可能会从缓存中获取它。

If you're still inside a transaction then only the session or connection that opened the transaction will be able to see the records. In some databases, you should see them from another session if you do a dirty/uncommitted read. I would try running a select using the same Hibernate session after the flush to verify that it really is in the database. Just don't query by the primary key or you may get it from the cache.

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