Java Hibernate - createNativeQuery 与“非实体类”结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19051058/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:42:15  来源:igfitidea点击:

Hibernate - createNativeQuery with "non-entity class" result

javahibernatejpa

提问by Joshua

I'm new to all this Hibernate/JPA stuff, so i will try to be as clear as possible.

我对所有这些 Hibernate/JPA 东西都不熟悉,所以我会尽量说清楚。

Is there any way in Hibernate to use createNativeQuery to select a single/or multiple fields in a query without using an Entity class as the return object ?

在 Hibernate 中是否有任何方法可以使用 createNativeQuery 在查询中选择单个/或多个字段而不使用实体类作为返回对象?

I'm trying to do this without using any XML related stuff.

我试图在不使用任何与 XML 相关的东西的情况下做到这一点。

Query query = getEntityManager().createNativeQuery("select name from contact where id_contact = :idContact", String.class);
query.setParameter("idContact", 9293L);
Object string = query.getSingleResult();
System.out.println(string);

Using this i have the Exception : Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.MappingException: Unknown entity: java.lang.String

使用这个我有例外: Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.MappingException: Unknown entity: java.lang.String

Thanks

谢谢

Edit :

编辑 :

I also tried :

我也试过:

Query query = getEntityManager().createNativeQuery("select name from contact where id_contact = :idContact");
query.setParameter("idContact", 9293L);
List list = query.getResultList();
if (!list.isEmpty()){ 
    Object string = list.get(0);
    System.out.println(string);
}

With the same Exception : Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object;

同样的例外: Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object;

Edit (2) : I'm starting to think it's either a bug in Hibernate or it's impossible to do such things...

编辑(2):我开始认为这要么是 Hibernate 中的错误,要么是不可能做这样的事情......

回答by Adam Dyga

Just try to call createNativeQuery()without passing String.class. If the namecolumn is of string-type in database query.getSingleResult()will actually return a String.

只是尝试调用createNativeQuery()而不通过String.class。如果name数据库中的列是字符串类型,query.getSingleResult()实际上将返回一个String.

回答by cyon

The problem is that you are passing String.classas the second parameter to createNativeQuery. This will make hibernate attempt to use the String.classto create a mapping for the result set. It can only create this mapping from entity classes and Stringis not an entity class because it isn't mapped to a table.

问题是您String.class作为第二个参数传递给createNativeQuery. 这将使 hibernate 尝试使用String.class为结果集创建映射。它只能从实体类创建此映射,并且String不是实体类,因为它没有映射到表。

Fortunately, the solution is to simply use an overloaded version of createNativeQuerythat doesn't require a second parameter.

幸运的是,解决方案是简单地使用createNativeQuery不需要第二个参数的重载版本。

String SQL = ".."; //same SQL as you had before
Query query = getEntityManager().createNativeQuery(SQL); //no entity mapping
query.setParameter("idContact", 9293L);
Object string = query.getSingleResult();
System.out.println(string);

回答by sunysen

try

尝试

Query query = getEntityManager().createNativeQuery("select name from contact where id_contact = :idContact", String.class);
query.setParameter("idContact", 9293L);
List list = query.getResultList();
if (!list.isEmpty()){ 
    Object string = list.get(0);
    System.out.println(string);
}

In the Look here

在看这里

http://sysout.be/2011/03/09/why-you-should-never-use-getsingleresult-in-jpa

http://sysout.be/2011/03/09/why-you-should-never-use-getsingleresult-in-jpa

回答by AjGupta

In case of Native query or jpql with column name EntityManager Returns a List of array of objects.

在本机查询或列名 jpql 的情况下,EntityManager 返回对象数组列表。

so to get Result List.

所以得到结果列表。

receive it in a

接收它

 List<Object[]> listResults = query.getResultList();

then iterate over it:-

然后迭代它:-

for (Object[] record : listResults) {

            //Iterate Logic will come here

                    }

回答by user7990606

String SQL = ".."; //same SQL as you had before
Query query = getEntityManager().createNativeQuery(SQL); //no entity mapping
query.setParameter("idContact", 9293L);
String string = (String)query.getSingleResult();
System.out.println(string);

回答by karepu

For Hibernate 5.0

对于休眠 5.0

Query query = getEntityManager().createNativeQuery(sql);
List<Object[]> objects = query.getResultList();
System.out.println(objects.get(0)[0]);

https://docs.jboss.org/hibernate/orm/5.0/userguide/html_single/chapters/query/native/Native.html

https://docs.jboss.org/hibernate/orm/5.0/userguide/html_single/chapters/query/native/Native.html