Spring Data JPA:Query如何返回非实体对象或对象列表? [英] Spring Data JPA: How can Query return Non- Entities Objects or List of Objects?
问题描述
我在我的项目中使用了spring数据JPA。我正在玩数百万条记录。我有一个要求,我必须为各种表获取数据并构建一个对象,然后在UI上绘制它。现在如何实现这个我的Spring数据仓库。我已经读过,它可以通过命名本地查询来实现。
如果指定的本地查询没有返回实体或$列表b $ b实体,我们可以使用@SqlResultSetMapping注释将查询结果映射到正确的返回类型
。
但是当我尝试使用 @SqlResultSetMapping
时,它正在使用另一个 entityResult 。意思是我所理解的仅仅是将一些查询结果转换为实体结果集,但我想要一个非实体对象的结果集。
@SqlResultSetMapping(
name =studentPercentile,
entities = {
@EntityResult(
entityClass = CustomStudent.class,
fields = {
@FieldResult(name =id,column =ID),
@FieldResult(name =firstName,column =FIRST_NAME),
@FieldResult(name =lastName ,column =LAST_NAME)
}
)
}
)
@NamedNativeQuery(
name =findStudentPercentile,
query =SELECT * FROM STUDENT,
resultSetMapping =studentPercentile)
我只是试图将学生实体的结果导入另一个不是实体的pojo' CustomStudent '中。 (这个例子我试图为POC目的执行,实际的用例很复杂,查询返回不同的结果集)。实现上面的用例?除了使用名称查询,我的存储库方法返回非实体对象还有其他方法吗?
当我但是,是的,您可以将使用@SqlResultSetMapping的查询结果映射到标量和托管实体。
我认为,是跳过自动映射。没有映射的查询会返回 List< Object []>
,您可以按照您的需要映射它。
I am using spring data JPA in my project. I am playing with millions of records. I have a requirement where I have to fetch data for various tables and build a object and then paint it on a UI. Now how to achieve this my Spring data Repositories. I have read that it can be achieved by Named native queries.
If the named native query does not return an entity or a list of entities, we can map the query result to a correct return type by using the @SqlResultSetMapping annotation.
But when I am trying to use @SqlResultSetMapping
it is taking another entityResult. Mean what I understand is that it is just transformation some query result to entity result set only, but I want a result set of non - entities objects.
@SqlResultSetMapping(
name="studentPercentile",
entities={
@EntityResult(
entityClass=CustomStudent.class,
fields={
@FieldResult(name="id", column="ID"),
@FieldResult(name="firstName", column="FIRST_NAME"),
@FieldResult(name="lastName", column="LAST_NAME")
}
)
}
)
@NamedNativeQuery(
name="findStudentPercentile",
query="SELECT * FROM STUDENT",
resultSetMapping="studentPercentile")
In above example I am just trying to get a results from student Entity into another pojo 'CustomStudent' which is not a entity. (This example I am trying to execute just for POC purpose, actual usecase is much complicated, with complicated query returning different resultset).
How to achieve above usecase? Is there any other way besides using name query that my repository method returning Non - Entities objects?
I was deeply surprised when I came accross this for the first time but, yes, you can map query results using @SqlResultSetMapping only to scalars and managed entities.
The best you can do, I guess, is to skip automatic mapping. Query without mapping would return List<Object[]>
and you can map it the way you need.
Another approach would be to use @MappedSuperclass. The class denoted as @MappedSuperclass (CustomStudent in your case) can be (not sure 100%, though) used in @SqlResultSetMapping. but you need to introduce inheritance hierarchy, that is your Student entity must extend CustomStudent. That would suck most of the time from the proper OO design, because inheritance would be a little bit artificial...
这篇关于Spring Data JPA:Query如何返回非实体对象或对象列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!