QueryDSL不能连接取嵌套子元素



无法获取join"/eager加载嵌套子元素。我们需要获取嵌套的子元素以避免N+1问题。最终得到org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list

我们有如下的伪数据模型(不能更改模型):

@Entity
@QueryEntity
public class PersonEntity {
@OneToOne(mappedBy = "person", fetch = FetchType.EAGER)
private FriendGroupEntity friendGroup;
}
@Entity
@QueryEntity
public class FriendGroupEntity {
@OneToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "PERSON_FK", nullable = false)
private PersonEntity person;

@OneToMany(mappedBy = "friendIn", fetch = FetchType.LAZY)
private List<FriendEntity> friends;
@OneToOne(mappedBy = "friendOwnerIn", fetch = FetchType.LAZY)
@Fetch(FetchMode.JOIN)
private FriendEntity friendOwner;
@OneToOne(mappedBy = "friendGroup", fetch = FetchType.LAZY)
private FriendGroupNameEntity groupName;
}
@Entity
@QueryEntity
public class FriendGroupNameEntity {
@OneToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "FRIEND_GROUP_FK", nullable = false)
private FriendGroupEntity friendGroup;
@Column(name = "name", length = 36, nullable = false)
private String name;
}
public class FriendEntity {
@OneToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "FRIEND_IN_FK", nullable = false)
@Fetch(FetchMode.JOIN)
private FriendGroupEntity friendIn;
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "FRIEND_OWNER_IN", nullable = false)
private FriendGroupEntity friendOwnerIn;
}

本质上我们想要急切获取的是a person的朋友组名+所有朋友和他们的朋友组名。

尝试使用这样的查询dsl:

JPAQuery<ParticipantEntity> query = jpaQueryFactory.selectFrom(person).distinct()
.innerJoin(person.friendGroup, friendGroup).fetchJoin()
.innerJoin(friendGroup.name, friendGroupName).fetchJoin()
.leftJoin(friendGroup.friendOwner, friend).fetchJoin()     
.leftJoin(friendGroup.friends, friend).fetchJoin()
.leftJoin(friend.friendIn, friendGroup).fetchJoin()
.leftJoin(friend.friendIn.name, friendGroupName).fetchJoin()
.fetch()

我们得到org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list。我们也尝试过使用。join(),然而问题是,当查看输出SQL时,我们可以看到,当fetchJoin()不使用时,嵌套关系最终没有被急切加载,因此不存在于"select"中。条款。

org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=friendGroupNameEntity,role=FriendGroupEntity.name,tableName=friend_group_name,tableAlias=person17_,origin=friend_group person16_,columns={person16_.part_pmt_sys_assoc_sk,className=FriendGroupNameEntity}}] [select distinct PersonEntity
from PersonEntity personEntity
inner join fetch personEntity.friendGroup as friendGroupEntity
inner join fetch friendGroupEntity.name as friendGroupNameEntity
left join fetch friendGroupEntity.friendOwner as friendOwnerEntity
left join fetch friendGroupEntity.friends as friendEntity
left join fetch friendEntity.friendIn as friendGroupEntity
left join fetch friendEntity.friendIn.name as friendGroupNameEntity

感谢您的帮助。

你可以试试这个,让我知道它是否有效?

JPAQuery<PersonEntity> query = jpaQueryFactory.selectFrom(person).distinct()
.innerJoin(person.friendGroup, friendGroup).fetchJoin()
.innerJoin(friendGroup.name, friendGroupName).fetchJoin()
.leftJoin(friendGroup.friendOwner, friendOwner).fetchJoin() // Changed from friend to friendOwner
.leftJoin(friendGroup.friends, friend).fetchJoin()
.leftJoin(friend.friendIn, friendGroup).fetchJoin()
.leftJoin(friend.friendIn.name, friendGroupName).fetchJoin()
.fetch();

最新更新