- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我们有一个项目需要延迟加载实体的集合,但在某些情况下我们需要急切加载它们。我们为我们的实体添加了一个 @NamedEntityGraph
注释。在我们的存储库方法中,我们添加了一个“javax.persistence.loadgraph”提示以急切地加载在所述注释中定义的 4 个属性。当我们调用该查询时,Hibernate 抛出 org.hibernate.loader.MultipleBagFetchException: cannot simultaneous fetch multiple bags
。
有趣的是,当我将所有这些集合重新定义为急切获取时,Hibernate 确实会急切获取它们,而不会出现 MultipleBagFetchException。
这里是提炼代码。实体:
@Entity
@NamedEntityGraph(name = "Post.Full", attributeNodes = {
@NamedAttributeNode("comments"),
@NamedAttributeNode("plusoners"),
@NamedAttributeNode("sharedWith")
}
)
public class Post {
@OneToMany(cascade = CascadeType.ALL, mappedBy = "postId")
private List<Comment> comments;
@ElementCollection
@CollectionTable(name="post_plusoners")
private List<PostRelatedPerson> plusoners;
@ElementCollection
@CollectionTable(name="post_shared_with")
private List<PostRelatedPerson> sharedWith;
}
查询方法(全部挤在一起以使其可发布):
@Override
public Page<Post> findFullPosts(Specification<Post> spec, Pageable pageable) {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Post> query = builder.createQuery(Post.class);
Root<Post> post = query.from(Post.class);
Predicate postsPredicate = spec.toPredicate(post, query, builder);
query.where(postsPredicate);
EntityGraph<?> entityGraph = entityManager.createEntityGraph("PlusPost.Full");
TypedQuery<GooglePlusFullPost> typedQuery = entityManager.createQuery(query);
typedQuery.setHint("javax.persistence.loadgraph", entityGraph);
query.setFirstResult(pageable.getOffset());
query.setMaxResults(pageable.getPageSize());
Long total = QueryUtils.executeCountQuery(getPostCountQuery(specification));
List<P> resultList = total > pageable.getOffset() ? query.getResultList() : Collections.<P>emptyList();
return new PageImpl<P>(resultList, pageable, total);
}
关于为什么这适用于实体级别的预取,但不适用于动态实体图,有什么提示吗?
最佳答案
我敢打赌,您认为有效的急切获取实际上工作不正常。
当您渴望获取多个“包”(允许重复的无序集合)时,用于执行渴望获取(左外连接)的 sql 将为连接的关联返回多个结果,如此 SO answer 所述。 .因此,当您急切地获取多个 List
时,hibernate 不会抛出 org.hibernate.loader.MultipleBagFetchException
,但由于上述原因,它不会返回准确的结果。
但是,当您为查询提供实体图提示时,hibernate 会(正确地)提示。 Hibernate developer, Emmanuel Bernard, addresses the reasons for this exception to be thrown :
eager fetching is not the problem per se, using multiple joins in one SQL query is. It's not limited to the static fetching strategy; it has never been supported (property), because it's conceptually not possible.
Emmanuel goes on to say in a different JIRA comment那个,
most uses of "non-indexed" List or raw Collection are erroneous and should semantically be Sets.
所以最重要的是,为了让多个急切的抓取按照你的意愿工作:
Set
而不是 List
@OrderColumn
注释保留 List
索引,FetchMode.SELECT
或 FetchMode.SUBSELECT
)编辑
相关:
关于java - NamedEntityGraph - JPA/Hibernate 抛出 org.hibernate.loader.MultipleBagFetchException : cannot simultaneously fetch multiple bags,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26934011/
我想在我的存储库层中有一个选项来预加载实体,所以我尝试添加一个方法来预加载具有所有关系的问题实体,但它会抛出 MultipleBagFetchException。我怎样才能解决这个问题?我正在使用 H
我们有一个 Lesson 实体,每个 Lesson 都有参加类(class)的学生和客人的列表: public class Lesson { @Id private Long id;
我们有一个 Lesson 实体,每个 Lesson 都有参加类(class)的学生和客人的列表: public class Lesson { @Id private Long id;
Hibernate 在创建 SessionFactory 期间抛出此异常: org.hibernate.loader.MultipleBagFetchException: can
我得到以下异常: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags 我
org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.loader.MultipleBagFetchExc
实体类 客户 @Entity @Table(name="Custumer") public class Custumer implements Seria
我有用户实体: @ToString @Data @Entity @Table(name = "users") @NamedEntityGraph(name = "UserWithItems",
我需要配置 spring + JPA (EntityManager) + Hibernate 。 如果我必须 fetch = FetchType.LAZY 运行服务器成功 如果我必须 fetch =
我正在将 WildFly 版本从 8.1 (Hibernate 4.3.5) 迁移到 11.0 (Hibernate 5.1.10),现在出现一个新错误: 引起:org.hibernate.loade
此错误不允许我访问 jsondoc,我已经尝试了此处提供的一些解决方案,但仍然遇到同样的问题。 我希望我已经足够清楚我想要你帮助我的事情。 我留下了错误以及错误中提到的类之一。 Cause
以下是我的代码在这里,我使用多个列表从数据库中获取数据。从 hql 查询中获取数据时显示异常。 Pojo 类 public class BillDetails implements java.io.S
我正在努力将应用程序从 Tomcat 8 迁移到 Weblogic 10.3.6。我们正在使用的技术堆栈: Spring :4.1.5.发布 hibernate :4.3.5 数据库:Postgres
我们有一个项目需要延迟加载实体的集合,但在某些情况下我们需要急切加载它们。我们为我们的实体添加了一个 @NamedEntityGraph 注释。在我们的存储库方法中,我们添加了一个“javax.per
我是一名优秀的程序员,十分优秀!