本文介绍了使用entityManager.createNativeQuery(query,foo.class)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从

javax.persistence.EntityManager.createNativeQuery 调用返回一个整数列表

javax.persistence.EntityManager.createNativeQuery call

为什么以下内容不正确?

Why is the following incorrect?

entityManager.createNativeQuery("Select P.AppID From P", Integer.class);

具体为什么我会得到...未知实体:java.lang.Integer

specifically why do I get "...Unknown entity: java.lang.Integer"

我是否必须创建一个具有整数字段的实体类?

Would I have to create an entity class that has a single field that is an Integer ?

谢谢

推荐答案

您所做的工作称为投影。当您只返回属于一个实体的标量值时。你可以用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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 14:40