我对嵌套列表投影界面的用法有疑问。我有两个实体(父母和孩子)(它们具有单向关联)
父母=>
@Table(name = "parent")
@Entity
public class ParentEntity {
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
// other fields........
}
Child =>
@Table(name = "child")
@Entity
public class ChildEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@NonNull
private String name;
@NonNull
@ManyToOne(fetch = FetchType.LAZY)
private ParentEntity parent;
// other fields........
}
我有两个用于选择特定列的投影界面。
ParentProjection =>
public interface ParentProjection {
String getName();
Set<ChildProjection> getChild();
}
ChildProjection =>
public interface ChildProjection {
String getId();
String getName();
}
我想带上ParentProjection列表,其中包括ChildProjection列表。
这样的存储库查询=>
@Query("select p.name as name, c as child from ParentEntity p left join ChildEntity as c on p.id = c.parent.id")
List<ParentProjection> getParentProjectionList();
此查询有效,但是它选择了ChildEntity的所有列,并且仅将id,名称属性映射到ChildProjection。 (生成的查询选择所有列,但我只想选择id和name列)
如何仅选择id和name列(为嵌套列表投影界面选择特定列)并映射到ChildProjection字段(与@Query一起使用?
注意:我不需要使用类类型投影。
您需要将OneToMany关系添加到ParentEntity并用Lazy进行注释。
希望它可以帮助((我已经尝试过)。