Java – namedentitygraph – JPA / hibernate.org hibernate. loader. Multiplebagfetchexception: cannot get multiple packages at the same time

We have a project where we need to load a collection of entities lazily, but in some cases we need them to load them eagerly We added the @ namedentitygraph annotation to the entity In our repository method, we added a "javax. Persistence. Loadgraph" prompt to eagerly load the four attributes defined in the annotation When we call the query, hibernate will throw org hibernate. loader. Multiplebagfetchexception: cannot get multiple packages at the same time

Interestingly, when I redefine all these collections as eager fetches, hibernate will eagerly fetch them without multiplebagfetchexception

This is the distillation code Entity:

@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;

}

Query method (everyone crowding together so that it can be published):

@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);
}

Any tips on why to use hot extract instead of dynamic solid graphics at the solid level?

Solution

I bet you think the job is right, but it's not right

When you are eager to obtain multiple "bags" (a set of unorders that allows repetition), the SQL used to execute the eager fetch (left outer connection) will return multiple results associated with the connection, as explained by so answer So although hibernate did not throw org hibernate. loader. Multiplebagfetchexception when you have multiple lists eager to get, it will not return accurate results for the reasons given above

However, hibernate will complain (correctly) when you provide entity diagram hints for queries Hibernate developer,Emmanuel Bernard,addresses the reasons for this exception to be thrown:

Emmanuel goes on to say in a different JIRA comment,

So the bottom line, in order for multiple aspirations to work as you wish:

>Use set instead of list > use JPA 2's @ ordercolumn annotation to preserve the list index, > if all other methods fail, fall back to hibernate specific extraction annotation (fetchmode. Select or fetchmode. Subselect)

edit

of

> https://stackoverflow.com/a/17567590/225217 > https://stackoverflow.com/a/24676806/225217

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>