开发者

HQL updates and domain objects

开发者 https://www.devze.com 2022-12-23 06:12 出处:网络
I have what may be a pretty elementary Hibernate question.Do HQL (and/or Criteria) update queries cause u开发者_开发知识库pdates to live domain objects?And do they automatically flush now-invalid doma

I have what may be a pretty elementary Hibernate question. Do HQL (and/or Criteria) update queries cause u开发者_开发知识库pdates to live domain objects? And do they automatically flush now-invalid domain objects from the first-level cache?

Example:

Player playerReference1 = session.get(Player.class,1);
session.createQuery("update players set gold = 100").executeUpdate();
//Question #1 -- does playerReference1.getGold() now return 100?
Player playerReference2 = session.get(Player.class,1);
//Question #2 -- does playerReference2.getGold() return 100, or is it the same exact object?

Should I make a practice of evicting all objects that are affected by an HQL update if there's a chance some code will need it later?


The answer to both questions is no. According to Java Persistence with Hibernate, chapter 12:

If you execute an SQL statement that operates directly on the rows in the database, any change you make doesn't affect the in-memory objects (in whatever state they may be). In other words, any direct DML statement bypasses the Hibernate persistence context (and all caches).

This applies to HQL as well. They recommend executing DML before loading any objects from the session.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号