Java JPA 原生查询返回类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9762730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA native query return class
提问by user595234
In the JPA, I defined a native sql which will return String,
在 JPA 中,我定义了一个将返回字符串的本机 sql,
@NamedNativeQuery(name = "alert",
query = " select distinct c.accountId from account c ",
resultClass = String.class)
the error message is
错误信息是
org.hibernate.MappingException: Unknown entity: java.lang.String
Any clues ? Thanks
任何线索?谢谢
回答by Andrey
Do not include resultClass
in query declaration
不包括resultClass
在查询声明中
回答by esej
@SqlResultSetMappings({
@SqlResultSetMapping(name = "alertMapping", columns = {
@ColumnResult(name = "accountId")})
})
@NamedNativeQuery(name = "alert",
query = " select distinct c.accountId from account c ",
resultSetMapping = "alertMapping")
Usage:
用法:
EntityManager em = ...... / injected / etc
TypedQuery<String> query = em.createNamedQuery("alert", String.class);
List<String> accountIds = query.getResultList();
(unchecked syntax, but I hope the basic idea comes through)
(未经检查的语法,但我希望基本思想通过)
For NamedNativeQueries you can only use resultClass when the result actually maps to an Entity. It's also possible to not specify a result mapping, in which case you'd get a List
of Object[] back. Each element of the list would be one record, and you'd have to explicitly cast each Object to the type you wanted. Hm, the last part might only be available to NativeQueries and not Named - sorry unsure at the moment.
对于 NamedNativeQueries,您只能在结果实际映射到实体时使用 resultClass。也可以不指定结果映射,在这种情况下,您将List
返回一个Object[] 。列表的每个元素都是一个记录,您必须将每个 Object 显式转换为您想要的类型。嗯,最后一部分可能只适用于 NativeQueries 而不是 Named - 抱歉目前不确定。
回答by DataNucleus
Seems Hibernate only allows Entity result classes. Obviously not all JPA implementations have this restriction. DataNucleus JPA, for example, allows that query to run fine.
似乎 Hibernate 只允许实体结果类。显然并不是所有的 JPA 实现都有这个限制。例如,DataNucleus JPA允许该查询正常运行。
回答by user1618928
In my case, I changed hibernate version from 3.5.6 to 4.3.3. it is working fine.
就我而言,我将休眠版本从 3.5.6 更改为 4.3.3。它工作正常。
<!-- <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.5.6-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.5.6-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency> -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.3.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.3.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.3.Final</version>
</dependency>