带有子查询的Spring JPA投影问题

问题描述 投票:0回答:1

我正在使用JPA投影,但是当我的查询包含子查询时,它将失败。例如:

public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "student_id")
    private Long id;

    private String name;

}

这里是投影的界面:

public interface StudentProjection {
    Integer getId();
}

和存储库:

@Query("select s from Student s where s.id = 88831")
List<StudentProjection> findProjections();

使用此代码,一切正常,回购返回EleveProjection的列表。

但是如果我将查询更改为此(例如,通过子选择获取ID-例如):

@Query("select s from Student s where s.id = (select max(88831) from Student)")
List<StudentProjection> findProjections();

...方法findProjections()返回org.springframework.data.jpa.repository.query.AbstractJpaQuery $ TupleConverter $ TupleBackedMap

的列表

并且显然不可能在此对象上执行getId()(抛出异常)。

它看起来像Spring数据JPA中的错误。我当前正在使用2.2.4.RELEASE版本。

关于如何将子查询与JPA投影一起使用的任何想法?

谢谢

java spring-boot spring-data-jpa subquery projection
1个回答
0
投票

@Query的内容尊重Jpql规范,因此与spring数据无关。

尝试一下@Query("select s from Student s where s.id in (select max(88831) from Student)") List<StudentProjection> findProjections();

AFAIK您可以将子句existsin与jpql中的子查询一起使用...

© www.soinside.com 2019 - 2024. All rights reserved.