简体   繁体   English

Hibernate - createNativeQuery带有“非实体类”结果

[英]Hibernate - createNativeQuery with “non-entity class” result

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在查询中选择单个/或多个字段而不使用Entity类作为返回对象?

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中的一个错误,或者不可能做这样的事情......

The problem is that you are passing String.class as the second parameter to createNativeQuery . 问题是您将String.class作为createNativeQuery的第二个参数createNativeQuery This will make hibernate attempt to use the String.class to create a mapping for the result set. 这将使hibernate尝试使用String.class为结果集创建映射。 It can only create this mapping from entity classes and String is not an entity class because it isn't mapped to a table. 它只能从实体类创建此映射,而String不是实体类,因为它未映射到表。

Fortunately, the solution is to simply use an overloaded version of createNativeQuery that 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);

In case of Native query or jpql with column name EntityManager Returns a List of array of objects. 如果是Native查询或带有列名的jpql,则EntityManager返回一个对象数组的List。

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

                    }

Just try to call createNativeQuery() without passing String.class . 只是尝试调用createNativeQuery()而不传递String.class If the name column is of string-type in database query.getSingleResult() will actually return a String . 如果name列在数据库query.getSingleResult()是string-type,则实际上将返回一个String

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

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);

For Hibernate 5.0 对于Hibernate 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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM