Persistence API: native query returning a list of Integers ?

Hi,

I have a query which returns a list of integers:

String query ="select a.id from A a where ..."

So I do the following:

Query q = em.createNativeQuery(query, java.lang.Integer.class);

q.setMaxResults(10);

List<Integer> ids = q.getResultList();

This gives me the following exception:

Exception [TOPLINK-6007] (Oracle TopLink Essentials - 2006.4 (Build 060412)): oracle.toplink.essentials.exceptions.QueryException

Exception Description: Missing descriptorfor [class java.lang.Integer].

Query: ReadAllQuery(java.lang.Integer)

at oracle.toplink.essentials.exceptions.QueryException.descriptorIsMissing(QueryException.java:403)

at oracle.toplink.essentials.queryframework.ObjectLevelReadQuery.checkDescriptor(ObjectLevelReadQuery.java:397)

at oracle.toplink.essentials.queryframework.ObjectLevelReadQuery.checkPrePrepare(ObjectLevelReadQuery.java:479)

at oracle.toplink.essentials.queryframework.ObjectLevelReadQuery.checkEarlyReturn(ObjectLevelReadQuery.java:418)

at oracle.toplink.essentials.queryframework.DatabaseQuery.execute(DatabaseQuery.java:556)

at oracle.toplink.essentials.queryframework.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:677)

at oracle.toplink.essentials.queryframework.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:731)

at oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2211)

at oracle.toplink.essentials.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:937)

at oracle.toplink.essentials.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:909)

at oracle.toplink.essentials.internal.ejb.cmp3.base.EJBQueryImpl.executeReadQuery(EJBQueryImpl.java:342)

at oracle.toplink.essentials.internal.ejb.cmp3.base.EJBQueryImpl.getResultList(EJBQueryImpl.java:430)

Why? Is it not possible to return integers?

Message was edited by:

batzee

Message was edited by:

batzee

[2167 byte] By [batzeea] at [2007-10-3 6:42:45]
# 1
i have a similar problem, or the same.could someone help?thanks, mauro
avonma at 2007-7-15 1:31:59 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Hi,

it looks like the current implementation is expecting an entity class to be passed as the second argument of em.createNativeQuery. This is useful if you want the implementation to create a entity instance from the result of the SQL query.

Did you try to omit the second argument, meaning use the overloaded createNativeQuery method that just takes the SQL query text?

Query q = em.createNativeQuery("SELECT a.id FROM A a WHERE ...");

Please note, the returned integers are enclosed in a Vector.

Another alternative is defining a simple SqlResultSetMapping. You could add the following to your class A:

@SqlResultSetMapping(

name="simpleIdMapping",

columns={ @ColumnResult(name="ID") } )

You pass the name of the SqlResultSetMapping as the second argument to the createNativeQuery call:

Query q = em.createNativeQuery("SELECT a.id FROM A a WHERE ...", "simpleIdMapping");

I hope this helps.

Regards Michael

bouschena at 2007-7-15 1:31:59 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
I am having the same problem. Could somebody provide full solution (without those ... in between)?
navrsalea at 2007-7-15 1:31:59 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

Hi,

You will need to do something like below:

public List<Myhelpclass> Myentityclass(String clID){

List<Myentityclass> myentityclass=null;

try{

myentity=(List<myentity>)em.createNamedQuery("SELECT i FROM Myentityclass i WHERE i.clID= :clID").setParameter("clID",clID).getResultList();

returncopyToDetails(myentity);

}catch (Exception ex) {

System.err.println("Can not get items details by ID entered");

throw new EJBException(ex);

}

}

You have to set the parameter as well.

eve

Message was edited by:

evepokua

evepokuaa at 2007-7-15 1:31:59 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

Hi,

You will need to do something like below:

public List<Myhelpclass> Myentityclass(String clID){

List<Myentityclass> myentityclass=null;

try{

myentity=(List<myentity>)em.createNamedQuery("SELECT i FROM Myentityclass i WHERE i.clID= :clID").setParameter("clID",clID).getResultList();

returncopyToDetails(myentity);

}catch (Exception ex) {

System.err.println("Can not get items details by ID entered");

throw new EJBException(ex);

}

}

You have to set the parameter as well.

eve

evepokuaa at 2007-7-15 1:31:59 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...