【发布时间】:2020-07-26 10:41:26
【问题描述】:
我正在使用 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();
使用此代码一切正常,并且 repo 返回一个 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 data JPA 中的一个错误。 我目前使用的是 2.2.4.RELEASE 版本。
关于如何使用 JPA 投影的子查询有什么想法吗?
谢谢
【问题讨论】:
-
我已经在 Spring Jira 上提交了测试用例。我是bug,我会测试ticket cmets中给出的解决方法。
标签: java spring-boot spring-data-jpa subquery projection