我正在尝试使用 Spring Data JPA Specificationiton 来查询数据,但我在这里遇到了一些问题。Java代码如下:
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;
});
当我注释代码“recipientNoticeJoin.fetch(UserNoticeEntity_.user, JoinType.INNER);”时,它工作正常,但是当我取消注释时,我会得到错误:
org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list
所以,我想知道使用规范方式是否支持join fetch,或者我的代码有问题。我知道使用@Query("some hql") 还有另一种方法,但不知何故我更喜欢使用规范方式。非常感谢。