java - 集合未从 hibernate/ehcache 二级缓存中读取

标签 java spring hibernate ehcache second-level-cache

我正在尝试在 Spring 项目中使用 ehcache/hibernate 缓存延迟加载的集合。当我执行 session.get(Parent.class, 123) 并多次浏览子项时,每次都会执行查询以获取子项。父级仅在第一次被查询,然后从缓存中解析。

可能我遗漏了什么,但我找不到解决方案。请参阅下面的相关代码。

我正在使用 Spring (3.2.4.RELEASE) Hibernate(4.2.1.Final) 和 ehcache(2.6.6)

父类:

@Entity
@Table(name = "PARENT")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, include = "all")

public class Parent implements Serializable {
/** The Id. */
    @Id
    @Column(name = "ID")
    private int id;


    @OneToMany(fetch = FetchType.LAZY, mappedBy = "parent")
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    private List<Child> children;

    public List<Child> getChildren() {
        return children;
    }

    public void setChildren(List<Child> children) {
        this.children = children;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Parent that = (Parent) o;
        if (id != that.id) return false;
        return true;
    }

    @Override
    public int hashCode() {
        return id;
    }
}

子类:

@Entity
@Table(name = "CHILD")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, include = "all")
public class Child {

    @Id
    @Column(name = "ID")
    private int id;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "PARENT_ID")
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    private Parent parent;

    public int getId() {
    return id;
    }

    public void setId(final int id) {
    this.id = id;
    }

    private Parent getParent(){
        return parent;
    }

    private void setParent(Parent parent) {
         this.parent = parent;
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
    }
        final Child that = (Child) o;
        return id == that.id;
    }

    @Override
    public int hashCode() {
        return id;
    }
}

应用上下文:

 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>Parent</value>
            <value>Child</value>
        </list>
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</prop>
            <prop key="hibernate.hbm2ddl.auto">validate</prop>
            <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
            <prop key="hibernate.connection.charSet">UTF-8</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.use_sql_comments">true</prop>

            <!-- cache settings ehcache-->
            <prop key="hibernate.cache.use_second_level_cache">true</prop>
            <prop key="hibernate.cache.use_query_cache">true</prop>
            <prop key="hibernate.cache.region.factory_class"> org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
            <prop key="hibernate.generate_statistics">true</prop>
            <prop key="hibernate.cache.use_structured_entries">true</prop>
            <prop key="hibernate.cache.use_query_cache">true</prop>
            <prop key="hibernate.transaction.factory_class"> org.hibernate.engine.transaction.internal.jta.JtaTransactionFactory</prop>
            <prop key="hibernate.transaction.jta.platform"> org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform</prop>
        </props>
    </property>
</bean>

我正在运行的测试用例:

@Test
public void testGetParentFromCache() {
  for (int i = 0; i <3 ; i++ ) {
      getEntity();
  }

}

private void getEntity() {
    Session sess = sessionFactory.openSession()
    sess.setCacheMode(CacheMode.NORMAL);
    Transaction t = sess.beginTransaction();

    Parent p  = (Parent) s.get(Parent.class, 123);
    Assert.assertNotNull(p);
    Assert.assertNotNull(p.getChildren().size());
    t.commit();
    sess.flush();
    sess.clear();
    sess.close();
}

在日志记录中,我可以看到第一次执行了 2 个查询,获取父项和子项。此外,日志显示子实体和集合都存储在二级缓存中。但是,当读取集合时,会执行查询以在第二次和第三次尝试时获取子项。

由于 EHCache 不工作,我们也尝试了 infinispan(具有不同的并发级别)。不幸的是,我们一直遇到同样的问题。

附言EHCache 论坛中也解决了这个问题:http://forums.terracotta.org/forums/posts/list/8785.page 和 hibernate 论坛:https://forum.hibernate.org/viewtopic.php?f=1&t=1029899

此外,我的同事在 GitHub 上创建了一个类似于我们的项目设置和问题的示例项目:https://github.com/basvanstratum/cacheimpl

最佳答案

此处的问题在于您的 Hibernate 版本。下面的代码将解释这里出了什么问题。

public class DefaultInitializeCollectionEventListener
      implements InitializeCollectionEventListener {
  public void onInitializeCollection(InitializeCollectionEvent event)
        throws HibernateException {
    …
    final boolean traceEnabled = LOG.isTraceEnabled();
    …

    final boolean foundInCache = methodReturningTrueWhenInCache();

    if ( foundInCache && traceEnabled ) {
      LOG.trace( "Collection initialized from cache" );
    }
    else {
      if ( traceEnabled ) {
        LOG.trace( "Collection not cached" );
      }
      methodThatExecutesAQuery();
  }
}

这是与此相关的 Hibernate 的 JIRA 票证的链接。解决方案是要么启用跟踪日志记录(糟糕——忘了我什至说过这个),要么将您的库升级到 4.2.2 或更高版本。 https://hibernate.atlassian.net/browse/HHH-8250

关于java - 集合未从 hibernate/ehcache 二级缓存中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19631639/

相关文章:

java - 在 spring mvc 中获取 400 错误

java - 使用 Spring Batch 处理文件顺序

java - 自定义 ClassLoader 在 JUnit 测试中代理静态库类

java - org.eclipse.swt.widgets.Composite setSize 无法正常工作

java - Spring WS 服务不通过 SOAPUI 响应任何请求

hibernate - 使用 Jersey 的 JAX-RS 的 hibernate 资源类中具有连接表的多对一

java - 在 hibernate 中使用 join 时映射实体类

java - JPA 2.0 双向 OneToOne 不在数据库中创建 FK 列

java - 使用Spring在项目目录中存储文件

JAVA HashSet 顺序