JPA Projection with custom collection property

狂风中的少年 提交于 2021-01-29 09:47:19

问题


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();
}

Projection as:

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

We get an error like this:

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

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

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

Thanks,


回答1:


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();
}


来源:https://stackoverflow.com/questions/56164938/jpa-projection-with-custom-collection-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!