使用 entityManager.createNativeQuery(query,foo.class) [英] use of entityManager.createNativeQuery(query,foo.class)
本文介绍了使用 entityManager.createNativeQuery(query,foo.class)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从一个
javax.persistence.EntityManager.createNativeQuery
调用
为什么以下不正确?
entityManager.createNativeQuery("Select P.AppID From P", Integer.class);
特别是为什么我得到...Unknown entity: java.lang.Integer"
specifically why do I get "...Unknown entity: java.lang.Integer"
我是否必须创建一个实体类,其中包含一个 Integer 字段?
Would I have to create an entity class that has a single field that is an Integer ?
谢谢
推荐答案
您所做的称为<strong>投影.那时您只返回属于一个实体的标量值.你可以用 JPA 做到这一点.请参阅标量值.
What you do is called a projection. That's when you return only a scalar value that belongs to one entity. You can do this with JPA. See scalar value.
我认为在这种情况下,完全省略实体类型是可能的:
I think in this case, omitting the entity type altogether is possible:
Query query = em.createNativeQuery( "select id from users where username = ?");
query.setParameter(1, "lt");
BigDecimal val = (BigDecimal) query.getSingleResult();
示例取自此处.
这篇关于使用 entityManager.createNativeQuery(query,foo.class)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文