代码之家  ›  专栏  ›  技术社区  ›  Paul Taylor

Hibernate会使用配置好的第二级EhCache来查找多个id吗

  •  2
  • Paul Taylor  · 技术社区  · 7 年前

    我已经为我的歌曲类配置了二级缓存,但是点击率不高,我想知道这是否是因为我通常按如下方式检索我的歌曲类。

     public static List<Song> getSongsFromDatabase(Session session, List<Integer> ids)
            {
                try
                {
                    List<Song> songs = session
                            .createCriteria(Song.class)
                            .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
                            .add(Restrictions.in("recNo", ids)).list();
                    return songs;
                }
                catch (Exception e)
                {
                    throw new RuntimeException(e);
                }
            }
    

    我想它只有在我使用查找的时候才起作用 身份证件 方法

    public static Song getSongFromDatabase(Session session, Integer id)
        {
            try
            {
                return (Song) session.get(Song.class, id);
            }
            catch (Exception e)
            {
                throw new RuntimeException(e);
            }
        }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Henri    7 年前

    这完全取决于二级缓存的配置方式。

    如果对实体进行了注释 @Cache ,实际上,它将在 session.get .

    对于查询,需要使其可缓存。这意味着在你的情况下要做这样的事情:

    List<Song> songs = session
      .createCriteria(Song.class)
      .setCacheable(true) // here
      .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
      .add(Restrictions.in("recNo", ids)).list();
    
    推荐文章