简体   繁体   中英

How can I know where the object came from, session cache or sessionFactory cache

In Hibernate we can have session cache & session factory level cache.

Now if both are enabled, and if I am getting an entity, then is there any way to know from where the entity came from? Whether it is from first level or second level cache. Do we have any method in Session to know that?

If you know the entity's identifier prior to querying for it, you can inspect the 2LC cache to determine whether that entity is at least cached in the SessionFactory scoped cache. You can do that by using the following method:

session.getSessionFactory().getCache().containsEntity( EntityClass.class, entityId );

You may be able to ask the 1LC by constructing a dummy instance of the entity, setting the identifier value and then calling the method:

session.contains( theEntityInstance );

Understand that under both of these scenarios, if you have issued a query for that particular entity with that identifier prior to calling either of these two methods, the entity will exist and the result of these two method calls will always be true .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM