具有自定义集合属性的JPA Projection [英] JPA Projection with custom collection property

查看:97
本文介绍了具有自定义集合属性的JPA Projection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Spring Data并尝试创建带有子查询的自定义查询,结果投影具有数组和其他属性,我们的问题出在子查询数组上.

We are using Spring Data and trying to create a custom query with a subquery, results projection have an array and other properties, our problem is with the subquery array.

    public interface ProfesionalRepository extends JpaRepository<Profesional, Long> {

    @Query("SELECT p.id as idProfesional, " +
            " p.name as name, " +
            " p.surname as surname, " +
            " (SELECT a.descripcionIlt FROM Ausencia a WHERE a.profesional.id = p.id) as exclusionesCenso " +
            " FROM Profesional p ")
    List<ProfesionalCensoProjection> findCenso();
}

投影为:

public interface ProfesionalCensoProjection {
    Long getIdProfesional();
    String getName();
    String getSurname();
    List<String> getExclusionesCenso();
}

我们收到这样的错误:

Caused by: java.sql.SQLException: ORA-01427: single-row subquery
returns more than one row

我们发现了其他帖子,例如: Spring JPA投影可以有集合吗?

We found other posts like: Can Spring JPA projections have Collections?

但是我们找不到带有子查询的示例. 如果JPA不允许,哪个是解决此问题的最佳解决方案?

But we cant find examples with subquery. If JPA doesn´t allow, which is the best solution for this problem?

谢谢

推荐答案

您尚未发布实体,但是Profesional似乎与Ausencia有关系.然后,您可以使用嵌套投影:

You have not posted the entities however Profesional appears to have a relationship to Ausencia. You can then use nested projection:

 public interface ProfesionalCensoProjection {
    Long getIdProfesional();
    String getName();
    String getSurname();
    // returns a projection which would include only the description
    List<AusenciaSumaryprojection> getExclusionesCenso(); 
}

public interface ProfesionalRepository extends JpaRepository<Profesional, Long> {

    List<ProfesionalCensoProjection> findCenso();
}

这篇关于具有自定义集合属性的JPA Projection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆