简体   繁体   English

使用非实体 class 作为结果 class 进行本机查询

[英]use a non-entity class as a result class for native query

every example/tutorials on the.net deals with entity classes, when it comes to stored procedures (functions), where the SP collects data only from this entity. .net 上的每个示例/教程都涉及实体类,当涉及存储过程(函数)时,SP 仅从该实体收集数据。 While it is quite common, that we have a complex query involving multiple tables and we want to have back some columns.虽然这很常见,但我们有一个涉及多个表的复杂查询,并且我们希望返回一些列。 For this we have can use a function in postgresql with or without any parameters.为此,我们可以在 postgresql 中使用带有或不带任何参数的 function。

I have this piece of code for calling such a function:我有这段代码可以调用这样的 function:

@Component
public class CapPlanningItemPerCapGroupImpl implements CapPlanningItemPerCapGroupRepository {
    private final EntityManager em;

    public CapPlanningItemPerCapGroupImpl(EntityManager em) {
        this.em = em;
    }

    @Override
    public List<CapPlanningItemPerCapGroup> getCapPlanningItems(Long companyId, Long plantId, int year, Long costAccTypeId) {
        Session session = em.unwrap(Session.class);
        NativeQuery<CapPlanningItemPerCapGroup> query = session.createNativeQuery(
                "SELECT * FROM get_cap_planning_items(:year, :companyId, :plantId, :costAccTypeId)", CapPlanningItemPerCapGroup.class);
        query.setParameter("year", year);
        query.setParameter("companyId", companyId);
        query.setParameter("plantId", plantId);
        query.setParameter("costAccTypeId", costAccTypeId);

        return query.getResultList();
    }
}

The CapPlanningItemPerCapGroup class isn't annotated with @Entity , since I don't want to store it as a datatable in the db. CapPlanningItemPerCapGroup class 没有用@Entity注释,因为我不想将它作为数据表存储在数据库中。 It's like a custom type.这就像一个自定义类型。 But with this setting, the above query fails, saying, that CapPlanningItemPerCapGroup is an unknown entity.但是使用此设置,上述查询失败,表示CapPlanningItemPerCapGroup是一个未知实体。

If I remove this CapPlanningItemPerCapGroup.class from the query call, I get the result in postman, but of course without any field names, just the raw data.如果我从查询调用中删除此CapPlanningItemPerCapGroup.class ,我将在 postman 中得到结果,但当然没有任何字段名称,只有原始数据。

So, my question is: do I really need to convert the result list manually to the appr.所以,我的问题是:我真的需要手动将结果列表转换为 appr. class type, or is there any mapping automatism out of the box, where I don't have to list all the returned column names and types? class 类型,或者是否有任何开箱即用的自动映射,我不必在其中列出所有返回的列名和类型?

Thanks.谢谢。

You can use an @SqlResultSetMapping to describe a constructor call that Hibernate will perform for every record in the result set.您可以使用@SqlResultSetMapping来描述 Hibernate 将对结果集中的每条记录执行的构造函数调用。 I explain that in detail in Result Set Mapping: Constructor Result Mappings on my blog.我在我的博客上的结果集映射:构造函数结果映射中对此进行了详细解释。

The general idea is simple:总体思路很简单:

Your CapPlanningItemPerCapGroup requires a constructor that sets all attributes.您的CapPlanningItemPerCapGroup需要一个设置所有属性的构造函数。

In the @SqlResultSetMapping , you describe the constructor call by referencing the class and listing the columns in your result set in the order in which you want to provide them to the constructor, eg:@SqlResultSetMapping中,您通过引用 class 并按照您希望将它们提供给构造函数的顺序列出结果集中的列来描述构造函数调用,例如:

@SqlResultSetMapping(
        name = "BookValueMapping",
        classes = @ConstructorResult(
                targetClass = BookValue.class,
                columns = {
                    @ColumnResult(name = "id", type = Long.class),
                    @ColumnResult(name = "title"),
                    @ColumnResult(name = "version", type = Long.class),
                    @ColumnResult(name = "authorName")}))

After you defined your mapping, you can provide its name as the 2nd parameter to the createNativeQuery method.定义映射后,您可以将其名称作为第二个参数提供给createNativeQuery方法。

List<BookValue> results = this.em.createNativeQuery("SELECT b.id, b.title, b.version, a.firstName || a.lastName as authorName FROM Book b JOIN Author a ON b.author_id = a.id", "BookValueMapping").getResultList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM