即使没有调用persist,JPA / Hibernate也会保存 [英] Does JPA/Hibernate save even when not calling persist

查看:138
本文介绍了即使没有调用persist,JPA / Hibernate也会保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

em.getTransaction().begin();

StringData sd = em.find(StringData.class, key);
System.out.println("Old value: " + sd.getData());
sd.setData(newValue);
// em.persist(sd);

em.getTransaction().commit();

正如您所看到的,我不会调用 persist ,它被注释掉了,因为我先干这个代码。然而,事实证明它并不是非常干燥。在检查数据库时,我发现数据发生了变化(幸运的是它是一个测试数据库)。

As you can see, I'm not calling persist, it's commented out, because I'm dry running this code first. However, as it turns out it's not so very dry. Upon inspecting the database, I see the data is changed (fortunately it's a test database).

显然,我对Hibernate / JPA的理解是有缺陷的。调用 persist 时总是需要更改数据吗?如果不是,什么时候什么规则保存了

Apparently my understanding of Hibernate/JPA is flawed. Isn't calling persist always required to change data? And if not, what are the rules on when something is saved?

推荐答案

在检测到任何更改时执行刷新(刷新也是通过提交完成)时会被保存,它被称为脏检查。

Yes, managed entities are saved when a flush (flush are also done with a commit) is done if any change is detected, it's called dirty checking.

这篇关于即使没有调用persist,JPA / Hibernate也会保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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