Hibernate使用第一级或第二级缓存加载所有实体 [英] Hibernate loading all entities utilizing 1st or 2nd level cache

查看:117
本文介绍了Hibernate使用第一级或第二级缓存加载所有实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一整个实体表,我们需要在休眠会话期间加载,我知道加载所有实体的唯一方式是通过HQL查询:

  public< T>列表与LT; T> getAllEntities(最终级< T> entityClass)方法{
如果(空== entityClass)方法
抛出新抛出:IllegalArgumentException( entityClass不能为空);

列表< T> list = castResultList(createQuery(
select e from+ entityClass.getSimpleName()+e).list());


返回列表;

我们使用EHcache进行二级缓存。



问题是在给定的交易时段中这被称为100次,并占用了总时间的相当一部分时间。有没有办法加载给定类型的所有实体(加载整个表),并仍然受益于一级会话缓存或二级ehcache。



我们已经告诉他们远离查询缓存,因为他们的潜在表现处罚相对于他们的收益。
* Hibernate查询缓存被认为是有害的



尽管我们现在正在执行性能分析,所以可能是时候尝试打开查询缓存。

解决方案

最后,我们通过在内存中存储我们需要加载的表中所有实体的主键(因为它们是模板数据并且没有添加/删除新模板)来解决此问题。然后,我们可以使用这个主键列表来查找每个实体,并利用Hibernates的第一级和第二级缓存。




We have an entire table of entities that we need to load during a hibernate session and the only way I know to load all entities is through an HQL query:

public <T> List<T> getAllEntities(final Class<T> entityClass) {
    if (null == entityClass)
        throw new IllegalArgumentException("entityClass can't be null");

    List<T> list = castResultList(createQuery(
            "select e from " + entityClass.getSimpleName() + " e ").list());


    return list;
}

We use EHcache for 2nd level caching.

The problem is this gets called 100's of times in a given transaction session and takes up a considerable portion of the total time. Is there any way to load all entities of a given type (load an entire table) and still benefit from 1st level session cache or 2nd level ehcache.

We've been told to stay away from query caching because of their potential performance penalties relative to their gains. * Hibernate Query Cache considered harmful

Although we're doing performance profiling right now so it might be time to try turning on query cache.

解决方案

We ended up solving this by storing in memory the primary keys to all the entities in the table we needed to load (because they're template data and no new templates are added/removed).

Then we could use this list of primary keys to look up each entity and utilize Hibernates 1st and 2nd level cache.

这篇关于Hibernate使用第一级或第二级缓存加载所有实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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