Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 798949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:02:59+00:00 2026-05-14T23:02:59+00:00

I have a JPA 2 Entity named Surgery . It has a member named

  • 0

I have a JPA 2 Entity named Surgery. It has a member named transfusionUnits that is an Integer.

There are two entries in the database. Executing this JPQL statement:

Select s.transfusionUnits from Surgery s

produces the expected result:

2
3

The following statement produces the expected answer of 5:

Select sum(s.transfusionUnits) from Surgery s

I expect the answer of the following statement to be 2.5, but it returns 2.0 instead.

Select avg(s.transfusionUnits) from Surgery s

If I execute the statement on a different (Float) member, the result is correct. Any ideas on why this is happening? Do I need to do some sort of cast in JPQL? Is this even possible? Surely I am missing something trivial here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:03:00+00:00Added an answer on May 14, 2026 at 11:03 pm

    First, let me summarize what the JPA 2.0 specification says about AVG (see section 4.8.5 Aggregate Functions in the SELECT Clause for the full content)

    The AVG function takes a state field path expression as an argument and calculates the average value of the sate field over the group. The state field must be numeric, and the result is returnd as a Double.

    So, after a first read, I was expecting the following snippet to pass:

    Query q = em.createQuery("select avg(s.transfusionUnits) from Surgery s");
    Double actual = (Double) q.getSingleResult();
    assertEquals(2.5d, actual.doubleValue());
    

    But it didn’t, I was getting 2.0 as result (a Double, but not the expected result).

    So I looked at the database level and realized that the SQL query was actually not returning 2.5. And indeed, this is what the documentation of my database says about AVG:

    The average (mean) value. If no rows are selected, the result is NULL. Aggregates are only allowed in select statements. The returned value is of the same data type as the parameter.

    I was expecting too much. JPA will return the result as a Double, but it won’t do any magic. If the query doesn’t return a result with the required precision, you won’t get it at the Java level.

    So without changing the type of transfusionUnits, I had to run this native query to get things working:

    Query q = em.createNativeQuery("SELECT AVG(CAST(s.transfusionUnits as double)) from Surgery s");
    Double actual = (Double) q.getSingleResult();
    assertEquals(2.5d, actual.doubleValue());
    

    Update: It appears that some database (e.g. MySQL) do return a value with a decimal part when using AVG on an INT column (which makes sense, regardless of the documented behavior of the database I used here). For other databases, the above cast could be handled
    in the “dialect” (e.g. see HHH-5173 for Hibernate) to avoid portability issues between database when using JPQL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A have a JPA entity that has timestamp field and is distinguished by a
I have a JPA Entity StatsEntity which has a composite primary key that is
I have a JPA object which has a many-to-many relationship like this: @Entity public
We have a project that uses JPA/Hibernate on the server side, the mapped entity
I have a JPA project connected to a MySQL database where my entity object
I have two JPA entities: @Entity public class TaskSchedule { ... private String name;
I would like to have an entity named GROUP in my JPA setup. Now
I have a JPA entity similar to; public class Inventory { private String productname;
I have entity model like this (using EclipseLink and JPA 2.0): @Entity class A
I have a some JPA entities that inherit from one another and uses discriminator

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.