当前位置: 首页 > 知识库问答 >
问题:

Spring Data JPA投影嵌套列表投影界面

陈宏胜
2023-03-14

我有一个关于嵌套列表投影界面用法的问题。我有两个实体(父和子)(它们有单向关联)

父级=>

@Table(name = "parent")
@Entity
public class ParentEntity {

    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String name;

    // other fields........
}

子=>

@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........
}

我有两个选择特定列投影界面。

public interface ParentProjection {

    String getName();

    Set<ChildProjection> getChild();
}
public interface ChildProjection {

    String getId(); 

    String getName();         
}
@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、name propeties映射到ChildProjection。(生成的查询选择所有列,但我想只选择id和name列)

我如何只选择id和name列(为嵌套列表投影界面选择特定列)并映射到ChildProjection字段(使用with@Query)?

注意:我不需要使用类类型投影。

共有1个答案

韩阳成
2023-03-14

您需要将OneTomany关系添加到ParentEntity并使用Lazy进行注释。

希望它有帮助(我已经试过了)

 类似资料:
  • 我有带有嵌套文档注释的帖子集合。简单的文档模式如下所示: 我只想投射所要求的评论。我找到了一个可以接受的解决方案。(也只欢迎使用注释对象返回解决方案) 然后我尝试将其应用于带有查询注释的mongoDB存储库接口。 但我得到了演员特例。有没有更好的方法来使用spring data mongodb实现这一点?

  • MongoDB 中的投影即查询指定的字段,而不是直接查询文档的全部字段。比如说某个文档中有 5 个字段,而我们只需要其中的 3 个字段,那么就可以使用 MongoDB 中的投影来指定需要查询的 3 个字段。 在《 MongoDB查询文档》一节中我们介绍的 find() 方法,在使用 find() 方法时,如果不设置其中的第二个参数,那么在查询时将返回文档中的所有字段,想要限制要查询的字段,您就需要

  • 举个简单的例子来说明正交投影与透视投影照相机的区别。使用透视投影照相机获得的结果是类似人眼在真实世界中看到的有“近大远小”的效果(如下图中的(a));而使用正交投影照相机获得的结果就像我们在数学几何学课上老师教我们画的效果,对于在三维空间内平行的线,投影到二维空间中也一定是平行的(如下图中的(b))。 (a)透视投影,(b)正交投影 那么,你的程序需要正交投影还是透视投影的照相机呢? 一般说来,对

  • 假设我有那些DTO: 在我的存储库中,我使用这些DTO作为投影进行查询: 我可以使用这个repo很好地访问ForumDTO中的id、name,但对于lasthread,它只返回null。我试过作为最后一个线程。Id、lastThread\u Id、lastThreadId均无效。

  • 当我尝试这个函数时,我得到一个错误。 CRSError:无效的投影: epsg: 4326:(内部程序错误:proj_create: SQLite错误对SELECT名称,类型,coordinate_system_auth_name,coordinate_system_code,datum_auth_name,datum_code,area_of_use_auth_name,area_of_use_

  • A Projector allows you to project a Material onto all objects that intersect its frustum. The material must use a special type of shader for the projection effect to work correctly - see the projector