将NativeQuery结果映射到POJO [英] Mapping NativeQuery results into a POJO
问题描述
我试图使用@SqlResultSetMapping和@ConstructorResult将本机查询的结果映射到POJO。这是我的代码:
@SqlResultSetMapping(name =foo,
classes = {
@ ConstructorResult(
targetClass = Bar.class,
columns = {
@ColumnResult(name =barId,type = Long.class),
@ColumnResult(name =barName ,type = String.class),
@ColumnResult(name =barTotal,type = Long.class)
})
})
public class酒吧{
私人长条形码;
私人字符串barName;
private long barTotal;
...
然后在我的DAO中:
查询查询= em.createNativeQueryBar(QUERY,foo);
...设置一些参数...
列表< Bar> list =(List< Bar>)query.getResultList();
我读过这个功能只支持在JPA 2.1中,但这就是我正在使用的功能。这是我的依赖关系:
< dependency>
< groupId> org.hibernate.javax.persistence< / groupId>
< artifactId> hibernate-jpa-2.1-api< / artifactId>
< version> 1.0.0.Final< / version>
< /依赖关系>
我发现了一些资源,包括这一个: jpa 2.1中的@ ConstructorResult映射。但我仍然没有任何运气。
我错过了什么?为什么不能找到SqlResultSetMapping?
javax.persistence.PersistenceException:org.hibernate.MappingException:Unknown SqlResultSetMapping [foo]
@SqlResultSetMapping
注释不应放在POJO 上。把它放在(任何) @Entity
类。 未知的SqlResultSetMapping [foo]告诉你,JPA提供者在名字'foo'下没有看到任何映射。请参阅我的另一个答案,正确的例子
在非实体类中加入两个表
I am attempting to map the results of a Native query to a POJO using @SqlResultSetMapping with @ConstructorResult. Here is my code:
@SqlResultSetMapping(name="foo",
classes = {
@ConstructorResult(
targetClass = Bar.class,
columns = {
@ColumnResult(name = "barId", type = Long.class),
@ColumnResult(name = "barName", type = String.class),
@ColumnResult(name = "barTotal", type = Long.class)
})
})
public class Bar {
private Long barId;
private String barName;
private Long barTotal;
...
And then in my DAO:
Query query = em.createNativeQueryBar(QUERY, "foo");
... set some parameters ...
List<Bar> list = (List<Bar>) query.getResultList();
I have read that this functionality is only supported in JPA 2.1, but that is what I am using. Here's my dependency:
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
I found a couple of resources, including this one: @ConstructorResult mapping in jpa 2.1. But I am still not having any luck.
What am I missing? Why can't the SqlResultSetMapping be found?
javax.persistence.PersistenceException: org.hibernate.MappingException: Unknown SqlResultSetMapping [foo]
@SqlResultSetMapping
annotation should not be put on a POJO. Put it at (any) @Entity
class. "Unknown SqlResultSetMapping [foo]" tells you, that JPA provider doesn't see any mapping under name 'foo'. Please see another answer of mine for the correct example
这篇关于将NativeQuery结果映射到POJO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!