Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring data jpa specification join fetch is not working

Tags:

I am trying to use Spring Data JPA Specificaiton to query data, but I got some problem here. The Java code is as below:

    List<NoticeEntity> studentNoticeEntityList = noticeRepository
            .findAll((root, criteriaQuery, criteriaBuilder) -> {
                criteriaQuery.distinct(true);

                root.fetch(NoticeEntity_.contentEntitySet, JoinType.LEFT);

                Predicate restrictions = criteriaBuilder.conjunction();

                SetJoin<NoticeEntity, UserNoticeEntity> recipientNoticeJoin = root
                        .join(NoticeEntity_.recipientNoticeEntitySet, JoinType.INNER);
                recipientNoticeJoin.on(criteriaBuilder.equal(
                        recipientNoticeJoin.get(UserNoticeEntity_.recipientStatus), NoticeRecipientStatus.Unread));
                Join<UserNoticeEntity, WeChatUserEntity> recipientUserJoin = recipientNoticeJoin
                        .join(UserNoticeEntity_.user);

                restrictions = criteriaBuilder.and(restrictions,
                        criteriaBuilder.equal(recipientUserJoin.get(WeChatUserEntity_.id), id));
                // recipientNoticeJoin.fetch(UserNoticeEntity_.user, JoinType.INNER);

                return restrictions;
            });

When I comment the code "recipientNoticeJoin.fetch(UserNoticeEntity_.user, JoinType.INNER);", it is working fine, but when I un-comment this, I will get error:

org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list 

So, I am wondering if join fetch is supported by using Specification way, or there is something wrong with my code. I know there is another way by using @Query("some hql"), but somehow I just prefer to use the Specification way. Thanks a lot.

like image 628
Ryan Wang Avatar asked Mar 27 '18 08:03

Ryan Wang


People also ask

Can JPQL join operations?

Instead of database table, JPQL uses entity object model to operate the SQL queries. Here, the role of JPA is to transform JPQL into SQL. Thus, it provides an easy platform for developers to handle SQL tasks. It can perform join operations.

How does @query work in spring boot?

The @Query annotation declares finder queries directly on repository methods. While similar @NamedQuery is used on domain classes, Spring Data JPA @Query annotation is used on Repository interface. This frees the domain classes from persistence specific information, which is a good thing.

Can we use inner join in JPQL?

In JPQL, JOIN can only appear in a FROM clause. The INNER keyword is optional (i.e. INNER JOIN is equivalent to JOIN).


1 Answers

The error specifies that you're missing an entity from your select list. Try this:

criteriaQuery.multiselect(root, root.get(NoticeEntity_.recipientNoticeEntitySet);

Also, hibernate may run a count query first to determine the number of results, and this can cause the above error. You can avoid this breaking by checking the return type of the query before adding the fetch.

Eager fetching in a Spring Specification

like image 69
Jeremy Ninnes Avatar answered Sep 19 '22 12:09

Jeremy Ninnes