我正在使用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投影一起使用的任何想法?
谢谢
@Query
的内容尊重Jpql规范,因此与spring数据无关。
尝试一下@Query("select s from Student s where s.id in (select max(88831) from Student)")
List<StudentProjection> findProjections();
AFAIK您可以将子句exists
和in
与jpql中的子查询一起使用...