Why does MySql return **Long**, how to get **Float**

i have query

select sum(rating)/count(*) from Rating r where r.studyCourseId=:id

when i try do this:

return em.createNamedQuery("getAverageRating", Float.class)
                .setParameter("id", studyCourseId)
                .getSingleResult();

Type specified for TypedQuery [Float] is incompatible with query return type [class java.lang.Long]

why does MySql return Long, how to get Float

Answer

One way to force float/double precision is to ensure that either the numerator or denominator be float/double:

select 1.0*sum(rating) / count(*) from Rating r where r.studyCourseId = :id

By multiplying the numerator by 1.0, we can force it to be float/double.