问题描述
我有一个实体定义如下:
I have an entity defined as follows:
public class Version {
@Id
private Long id;
private String content;
@Transient
private Model model;
//...
}
从我的内容可以看到,当在实体管理器上执行 find
操作时,它只在基础数据库上生成一次 SELECT
,然后将实体缓存在实体管理器中。但是,我看到如果我将 Model
分配给模型
属性,则此更改不会反映到缓存的实体。例如。如果在一次通话中,找到
操作已完成,并且模型
已分配,当我执行时再次从另一个EJB中找到
, model
属性再次为 null
。此更改是否未反映到缓存实体?也许是因为它是 @Transient
?
From what I can see, when a find
operation is done on Entity Manager, it makes a SELECT
on the underlying database only once, and then the entity is cached in the Entity Manager. However, I see that if I assign a Model
to the model
property, this change is not reflected to the cached entity. E.g. if in one call, a find
operation is done and Model
is assigned, when I do find
again from another EJB, model
property is null
again. Is this change not reflected to the cached entity? Perhaps because it's @Transient
?
推荐答案
实体经理维持第一级别缓存,一旦事务结束,该第一级缓存就会被丢弃。否则,缓存将返回过时值,因为在同一应用程序或另一个应用程序中的其他事务可以修改或删除缓存的实体。
The entity manager maintains a first level cache, and this first level cache is thrown away as soon as the transaction has ended. Else, the cache would return stale values, since other transactions, in the same application or in another one, could modify or remove the cached entities.
此外,并发事务每个都有自己的会话级缓存,因此它们自己是同一个实体的实例。
Moreover, concurrent transactions each have their own session-level cache, and thus their own instance of the same entity.
如果在后续事务中,您找到
同一个实体,将发出新的SQL查询,并返回该实体的不同实例。
If in a subsequent transaction, you find
the same entity, a new SQL query will be issued, and a different instance of the entity will be returned.
如果某个实体的事务必须记住某些事情,那么它应该在数据库中保持不变。这就是数据库的重点。
If something must be remembered across transactions for a given entity, then it should be made persistent in in the database. That's the point of a database.
这篇关于JPA EntityManager缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!