Hibernate - 使用“非实体类”创建自定义查询结果 [英] Hibernate - createNativeQuery with "non-entity class" result
问题描述
在Hibernate中有没有什么方法可以使用createNativeQuery在查询中选择单个或多个字段而不使用实体类作为返回对象?
我试图在不使用任何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);
使用此函数我有例外:线程mainjavax中的异常。 persistence.PersistenceException:org.hibernate.MappingException:未知实体:java.lang.String
谢谢
编辑:
我也试过:
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);
$ / code>
使用相同的异常:线程中的异常main java.lang.ClassCastException:java.lang.String不能转换为[Ljava.lang.Object;
编辑(2):
我开始认为这是Hibernate中的一个bug,或者做这种事情是不可能的......
问题在于你传递 String.class
作为 createNativeQuery
的第二个参数。这会让休眠尝试使用 String.class
来创建结果集的映射。它只能从实体类创建映射,并且 String
不是实体类,因为它没有映射到表。
$ b $幸运的是,解决方案是简单地使用不需要第二个参数的
String SQL =..; //与
之前的SQL具有相同的SQL查询query = getEntityManager()。createNativeQuery(SQL); //没有实体映射
query.setParameter(idContact,9293L);
Object string = query.getSingleResult();
System.out.println(string);
I'm new to all this Hibernate/JPA stuff, so i will try to be as clear as possible.
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 ?
I'm trying to do this without using any XML related stuff.
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
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;
Edit (2) : I'm starting to think it's either a bug in Hibernate or it's impossible to do such things...
The problem is that you are passing String.class
as the second parameter to createNativeQuery
. This will make hibernate attempt to use the String.class
to create a mapping for the result set. It can only create this mapping from entity classes and String
is not an entity class because it isn't mapped to a table.
Fortunately, the solution is to simply use an overloaded version of createNativeQuery
that doesn't require a second parameter.
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);
这篇关于Hibernate - 使用“非实体类”创建自定义查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!