Spring Data JPA:查询如何返回非实体对象或对象列表? [英] Spring Data JPA: How can Query return Non- Entities Objects or List of Objects?

查看:50
本文介绍了Spring Data JPA:查询如何返回非实体对象或对象列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用 spring 数据 JPA.我正在玩数百万条记录.我有一个要求,我必须为各种表获取数据并构建一个对象,然后在 UI 上绘制它.现在如何实现我的 Spring 数据存储库.我已经读过它可以通过命名本机查询来实现.

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.

如果命名的原生查询没有返回实体或列表实体,我们可以通过以下方式将查询结果映射到正确的返回类型使用@SqlResultSetMapping 注解.

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.

但是当我尝试使用 @SqlResultSetMapping 时,它正在使用另一个 entityResult.意思是我的理解是它只是将一些查询结果转换为实体结果集,但我想要一个非实体对象的结果集.

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")

在上面的示例中,我只是想将结果从学生实体获取到另一个不是实体的 pojo 'CustomStudent'.(这个例子我只是为了 POC 目的而尝试执行,实际用例非常复杂,复杂的查询返回不同的结果集).

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?

推荐答案

当我第一次遇到这个问题时,我深感惊讶,但是,是的,您只能将使用 @SqlResultSetMapping 的查询结果映射到标量和托管实体.

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.

我猜你能做的最好的事情就是跳过自动映射.没有映射的查询将返回 List,您可以按需要的方式映射它.

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.

另一种方法是使用@MappedSuperclass.表示为@MappedSuperclass(在您的情况下为CustomStudent)的类可以(虽然不确定100%)在@SqlResultSetMapping 中使用.但是您需要引入继承层次结构,即您的 Student 实体必须扩展 CustomStudent.从正确的 OO 设计中大部分时间会很糟糕,因为继承会有点人为......

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:查询如何返回非实体对象或对象列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆