-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Imcompatible return types on Hibernate 3.5.1 as JPA2 impl
PostPosted: Thu May 27, 2010 1:53 pm 
Beginner
Beginner

Joined: Fri Sep 02, 2005 3:42 pm
Posts: 32
Hi, i'm using Hibernate 3.5.1 as JPA2 implementation. I'm trying to create a query tha uses AVG function. My problem is: i have a BigDecimal attribute but when i use avg(value), it returns a Double and a exceptin is throwed. Here is my code:

Code:
   public BigDecimal calculaTotalMovimentado(Conta conta, TipoMovimentacao tipo) {
      String jpql = "select avg(valor) from Movimentacao where conta=:pConta and"
            + " tipo=:pTipo";
      TypedQuery<BigDecimal> query = this.em.createQuery(jpql,BigDecimal.class);
      query.setParameter("pConta", conta);
      query.setParameter("pTipo", tipo);
      return query.getSingleResult();

   }


When i try to run a get this exception:
Code:
Exception in thread "main" java.lang.IllegalArgumentException: Type specified for TypedQuery [java.math.BigDecimal] is incompatible with query return type [class java.lang.Double]


The "valor" attribute is a BigDecimal. Is this a bug or my mistake ?

Thanks,

Alberto


Top
 Profile  
 
 Post subject: Re: Imcompatible return types on Hibernate 3.5.1 as JPA2 impl
PostPosted: Thu May 27, 2010 5:41 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
It seems like the AVG method is expected to return a double according to the JPA specification.

Quote:
The Java type that is contained in the result of a query using an aggregate function is as follows:
• COUNT returns Long.
• MAX, MIN return the type of the state-field to which they are applied.
• AVG returns Double.
• SUM returns Long when applied to state-fields of integral types (other than BigInteger); Double
when applied to state-fields of floating point types; BigInteger when applied to state-fields
of type BigInteger; and BigDecimal when applied to state-fields of type BigDecimal.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.