PostgreSQL function round and JPA/Hibernate -
i have query executed java application this:
query query = getentitymanager().createquery(hql); the query looks this:
string hql = "select * table round(column1, 3) = round(parameter, 3)"; here column1 of type double. value holds 143.02856666. need retain value is, business logic need round , compare.
the initial database configured h2 , worked fine. database has been changed postgres , query errors out.
error: function round(double precision, integer) not exist hint: no function matches given name , argument types. might need add explicit type casts.
the round() function in postgres takes numeric datatype , needs cast.
the below query works fine if executed directly in postgres console.
select * table round(cast(column1 numeric), 3) = round(cast(parameter numeric), 3); the same java application errors out.
java.lang.illegalargumentexception: org.hibernate.queryexception: not resolve requested type cast : numeric
also tried query query = getentitymanager().createnativequery(hql); results in new error.
org.hibernate.engine.jdbc.spi.sqlexceptionhelper - error: syntax error @ or near "where"
if debug, errors out when below line executed.
list resultlist = query.getresultlist(); how rewrite query works against postgres ?
what doing query query = getentitymanager().createquery(hql); calling jpql-query, not support db-functions round(v numeric, s integer).
two suggestions:
- use
between, maintain jpql-mapping - write nativequery ->
query query = em.createnativequery(querystring);
your querystring has altered parameters.
Comments
Post a Comment