Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring JPA-EntityGraph和Hibernate二级缓存(ehcache)_Spring_Hibernate_Jpa_Ehcache - Fatal编程技术网

Spring JPA-EntityGraph和Hibernate二级缓存(ehcache)

Spring JPA-EntityGraph和Hibernate二级缓存(ehcache),spring,hibernate,jpa,ehcache,Spring,Hibernate,Jpa,Ehcache,我的一对多关系定义如下 @Cacheable @Entity @NamedEntityGraph( name = "Parent.Child", attributeNodes = { @NamedAttributeNode("children"), } ) public class Parent { private Set<Child> children; // getter - setter } @可缓存 @实体 @姓名识别

我的一对多关系定义如下

@Cacheable
@Entity
@NamedEntityGraph(
    name = "Parent.Child",
    attributeNodes = {
        @NamedAttributeNode("children"),
    }
)
public class Parent {
    private Set<Child> children;
    // getter - setter
}
@可缓存
@实体
@姓名识别图(
name=“Parent.Child”,
属性节点={
@名为“儿童”,
}
)
公共类父类{
私人儿童;
//吸气剂设定器
}
现在在我的DAL中,我调用这个方法

@Override
    public Parent getParentWithChildren(int id) {
        EntityGraph<?> graph = entityManager.getEntityGraph("Parent.Child");
        Map<String, Object> props = new HashMap<>();
        props.put("javax.persistence.fetchgraph", graph);
        return entityManager.find(Parent.class, id, props);
    }
@覆盖
公共父项getParentWithChildren(int id){
EntityGraph graph=entityManager.getEntityGraph(“Parent.Child”);
Map props=newhashmap();
put(“javax.persistence.fetchgraph”,graph);
返回entityManager.find(Parent.class、id、props);
}
由于我已向父级加载了子级,因此我应该能够在事务之外使用子级集合。但是我越来越懒了。只有在启用hibernate 2级缓存-ehcache时才会发生这种情况。如果我从配置中禁用它,它将按预期工作。另外,如果我在查找之后显式初始化集合,它将按预期工作。那它是虫子吗?。我在JPA2.1中使用Hibernate 5.2.6.Final


编辑:我注意到的另一件事是,实体首次加载正常,所以这个问题一定与hibernate&cache provider有关。

否。这不是一个bug。我猜Hibernate将从缓存加载延迟到真正需要时。除非您要求即时抓取,否则实现可以决定视情况而定为懒惰


因此,一般规则是在退出事务之前(准确地说是在关闭Hibernate会话之前)加载所需的所有内容。

不,这不是一个bug。我猜Hibernate将从缓存加载延迟到真正需要时。除非您要求即时抓取,否则实现可以决定视情况而定为懒惰


因此,一般规则是在退出事务之前(准确地说,在关闭Hibernate会话之前)加载所需的所有内容.

我已经在使用fetch graph急切地加载子集合。您没有提供足够的信息让我证明这一点,但我怀疑实际上是集合中的对象被代理延迟加载。请参阅props对象。我正在指定fetch graph我已经在使用fetch graph急切地加载子集合您没有提供足够的信息让我证明这一点,但我怀疑实际上是集合中的对象被代理延迟加载。请参阅props对象。我想我也经历过类似的情况。我不使用二级缓存。我意识到,若实体已经在持久性上下文中,则next em.find with fetchgraph hint不会从数据库中加载任何内容。在我看来,这也是一个bug。我认为会话缓存也会发生同样的情况。这是一个已确认的错误,并在5.2.10I中标记为待解决。我遇到类似情况。我不使用二级缓存。我意识到,若实体已经在持久性上下文中,则next em.find with fetchgraph hint不会从数据库中加载任何内容。在我看来,这也是一个bug。我认为会话缓存也会发生同样的情况。这是一个已确认的错误,在5.2.10中标记为要解决