简体   繁体   English

如何映射从休眠中的本地查询得到的结果?

[英]How to map result got from native query in hibernate?

I have a query that has more columns then what my entity class has. 我有一个查询,该查询具有比我的实体类所拥有的更多的列。

In order to not let hibernate complaints, I have to add an annotation to the field like 为了不让冬眠的抱怨,我必须在字段中添加注释,例如

@Transient
private Integer count;

But by doing this makes hibernate not able to map count. 但是这样做会使休眠状态无法映射计数。 Let's say my query is 假设我的查询是

session.createSQLQuery("SELECT p.*, count(p.id), sqrt(1+2) as distance FROM post p group by p.id")

I know the query doesn't make any logical sense. 我知道查询没有任何逻辑意义。 This is just for example. 这只是举例。 The columns return from query above will have everything in post and two extra columns, count and distance. 从上面的查询返回的列将包含所有内容,另外还有两个列,即count和distance。 I wanted to map the result to my entity with count and distance are annotated with @Transient , or if there's a better way to map the result. 我想将结果映射到我的entity ,并用@Transient注释计数和距离,或者是否有更好的方法来映射结果。 I'm more than happy to do so. 我很高兴这样做。 The goal is not to do this in an entity but a class with mapped result. 目标不是在实体中执行此操作,而是在具有映射结果的class执行此操作。 I've tried calling addEntity() but doesn't seem to help. 我试过调用addEntity()但似乎无济于事。

You can use Result Set Transformer to achieve this. 您可以使用结果集转换器实现此目的。

Step 1 ) Create a new DTO class with all the fields that you query going to return 步骤1)使用您要查询的所有字段返回一个新的DTO类

Step 2 ) Add the below line 步骤2)添加以下行

setResultTransformer( Transformers.aliasToBean(DTO.class))

Example : 范例:

List resultWithAliasedBean = session.createQuery(
  "SELECT p.*, count(p.id), sqrt(1+2) as distance FROM post p group by p.id")
  .setResultTransformer(Transformers.aliasToBean(DTO.class))
  .list();

DTO dto = (DTO) resultWithAliasedBean.get(0);

Note : Make sure the field names in the DTO class match the column name which your query is returning. 注意:确保DTO类中的字段名称与查询要返回的列名称匹配。

I see that you are using Hibernate so Yathish answer works fine. 我看到您使用的是Hibernate,因此Yathish答案可以正常工作。

But if you want to do it with JPA spec then you can use Result Set Mapping 但是,如果要使用JPA规范,则可以使用结果集映射

Query q = em.createNativeQuery(
    "SELECT c.id, c.name, COUNT(o) as orderCount, AVG(o.price) AS avgOrder " +
    "FROM Customer c " +
    "JOIN Orders o ON o.cid = c.id " +
    "GROUP BY c.id, c.name",
    "CustomerDetailsResult");

@SqlResultSetMapping(name="CustomerDetailsResult",
    classes={
        @ConstructorResult(targetClass=com.acme.CustomerDetails.class,
            columns={
                @ColumnResult(name="id"),
                @ColumnResult(name="name"),
                @ColumnResult(name="orderCount"),
                @ColumnResult(name="avgOrder", type=Double.class)})
    })

There you have to specifiy the mappin of the columns from the SQL result set to the DTO. 在那里,您必须指定从SQL结果集到DTO的列的映射。

And if you think this is to complicated there is a open source project called QLRM (Query Lanaguage Result Mapper) that mapps any SQL statement to a POJO. 而且,如果您认为这很复杂,那么可以使用一个名为QLRM(查询语言结果映射器)的开源项目,该项目将任何SQL语句映射到POJO。

http://simasch.github.io/qlrm/ http://simasch.github.io/qlrm/

And last but not least if you will do extensive SQL processing why not have a look at jOOQ: https://www.jooq.org/ 最后但并非最不重要的一点是,如果您要进行大量的SQL处理,为什么不看看jOOQ: https ://www.jooq.org/

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

相关问题 如何将本机查询结果映射到Java类 - How to Map Native Query result to java Class 无法从Hibernate本机查询的结果集中读取列值 - could not read column value from result set in Hibernate Native Query 从Hibernate本机查询的结果中获取动态生成的列名 - Getting dynamically generated column names from result of Hibernate native query Map 值从 Hibernate 本机查询返回到 model - Map values returned from Hibernate Native Query to a model 如何知道Hibernate中本机SQL查询结果的类型? - How to know the type of a native SQL query result in Hibernate? Hibernate / JPA将本机查询的结果映射到非实体持有实体 - Hibernate/JPA map Result of native Query to non-Entity holding Entities 如何使用 JPA 存储库将涉及来自多个表的列的本机查询结果的结果 map 转换为自定义 class? - How to map the result from a native query result involving columns from multiple table into a Custom class using JPA Repository? 使用休眠本机查询的BigInteger结果错误 - Wrong BigInteger result using hibernate native query 如何将本机查询的结果集映射到实体中的变量 - How to Map Result Set of Native Query to Variable in Entity Hibernate:使用@SqlResultSetMapping映射本机查询的结果集 - Hibernate: Mapping result set of native query with @SqlResultSetMapping
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM