I would like to return a List of Integers from a
javax.persistence.EntityManager.createNativeQuery
call
Why is the following incorrect?
entityManager.createNativeQuery("Select P.AppID From P", Integer.class);
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 ?
Thanks
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:
Example taken from here.
JPA was designed to provide an automatic mapping between Objects and a relational database. Since Integer is not a persistant entity, why do you need to use JPA ? A simple JDBC request will work fine.
That doesn't work because the second parameter should be a mapped entity and of course Integer is not a persistent class (since it doesn't have the @Entity annotation on it).
for you you should do the following:
or if you want to use HQL you can do something like this:
Regards.
Suppose your query is "select id,name from users where rollNo = 1001".
Here query will return a object with id and name column. Your Response class is like bellow:
here
UserObject
constructor will get a Object Array and set data with object.Your query executing function is like bellow :
Here you have to import bellow packages:
Now your main class, you have to call this function. First you have to get EntityManager and call this
getUserByRoll(EntityManager entityManager,String rollNo)
function. Calling procedure is given bellow:Now you have data in this userObject.
Here is Imports
Note:
query.getSingleResult() return a array. You have to maintain the column position and data type.
query return a array and it's
[0] --> id and [1] -> name
.For more info, visit this Answer
Thanks :)
Here is a DB2 Stored Procidure that receive a parameter
SQL
Java