简体   繁体   English

Hibernate Session.multiLoad L2缓存问题

[英]Hibernate Session.multiLoad L2 cache issue

I'm using Hibernate 5.2.6 with the configured L2 cache (via jhache/ehcache). 我正在使用Hibernate 5.2.6和配置的L2缓存(通过jhache / ehcache)。 I want to load multiple entities by id 我想通过id加载多个实体

session.byMultipleIds(User.class).multiLoad(ids)

After the first call is see 看到第一个电话后

select user0_.id as id1_20_0_ ... from user_data user0_ where user0_.id in (?,...,?)

in the log and User entities are placed to the L2 cache. 在日志中,用户实体被放置到L2缓存中。

The second call triggers the same SQL statement in the log and isn't retrieving entities from L2 cache. 第二个调用在日志中触发相同的SQL语句,而不是从L2缓存中检索实体。

With the L1 cache this issue doesn't occur (with enableSessionCheck(true) ). 使用L1缓存时,不会发生此问题(使用enableSessionCheck(true) )。

Is it expected behavior or I've misconfigured something? 这是预期的行为还是我错误配置了什么?

I've worked it around with helper class: 我用辅助类来解决它:

public class HibernateUtils {

    static <T> List<T> byMultipleIds(Session session, Long[] ids, Class<T> entityClass) {
        List<Long> notCached = new ArrayList<>();
        for (Long id : ids) {
            if (!session.getSessionFactory().getCache().contains(entityClass, id)) {
                notCached.add(id);
            }
        }
        return session.byMultipleIds(entityClass).enableOrderedReturn(false).withBatchSize(ids.length).multiLoad(notCached);
    }

}

But it doesn't looks like good solution 但它看起来不是一个好的解决方案

This has now been fixed in with https://hibernate.atlassian.net/browse/HHH-12944 which is available in Hibernate 5.4.0.CR1. 现在已经使用https://hibernate.atlassian.net/browse/HHH-12944修复了这个问题,它可以在Hibernate 5.4.0.CR1中找到。

Detailed documentation can be found at http://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#pc-by-multiple-ids . 详细文档可以在http://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#pc-by-multiple-ids找到。

Assuming you want Users identified by 1L, 2L, and 3L you can now use: 假设您希望由1L,2L和3L标识的用户现在可以使用:

session.byMultipleIds(User.class).enableSessionCheck(true).multiLoad( 1L, 2L, 3L);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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