• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with a native query in JPA

 
Ela Jarecka
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
maybe someone could point me in the right direction..

I have a native query:
"SELECT COUNT(1) FROM VERPFLICHTUNG v WHERE v.ID_FORDERUNG = ? AND v.LFDNRVERPFLICHTUNG = ?"

I expect it to return a single value, so I am trying to call like that:
em.createNativeQuery( queryClaimSerialNumber ).setParameter( 1, claimNumber ).setParameter( 2, serialNumber ).getSingleResult();

getSingleResult is supposed to return an Object, but I get a Vector. What am I doing wrong?
I find many examples in internet along the lines of
----------------------
Query query = em.createNativeQuery( "select id from users where username = ?" );
query.setParameter(1, "lt");
BigDecimal val = (BigDecimal) query.getSingleResult();
----------------------

when I try to cast the result, I obviously get "java.util.Vector cannot be cast to xxx"

I am working with Netbeans 6.7, JEE 1.5.

Thanks in advance for your help.
 
Ela Jarecka
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This seems to work:

Vector singleResult = ( Vector ) this.em.createNativeQuery( queryClaimSerialNumber ).
setParameter( 1, claimNumber ).setParameter( 2, serialNumber ).
getSingleResult();
BigDecimal result = ( BigDecimal ) singleResult.get( 0 );
if ( result.shortValue() == 1 ) {
....
}

but I would still love to understand why..
 
Ankit Garg
Sheriff
Posts: 9577
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't understand your query. you are using count(1) in the select statement. That will return the number of rows resulted by your query. Is this actually what you want. A JPA query using count returns a Long as a singleResult. I'm clueless about this, maybe someone else knows something...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic