[英]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.