简体   繁体   中英

JPA Hibernate Postgres precision doesn't work

I am trying to run query below , Postgres database.

  select new map(avg(cast (speed as double precision)) as avg)
  from table 

speed column is of type varchar

I am executing using JPA as below

        em = entityManagerFactory.createEntityManager();        
        Query hqlQuery = em.createQuery(query);
        reportList = hqlQuery.getResultList();

When I run this I get error below

    Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: expecting CLOSE, 
    found 'precision' near line 1

Has anyone come across this situation? Can I not use precision in Hibernate?

If you're trying to execute native SQL in Hibernate via JPA, you need to use em.createNativeQuery .

em.createQuery expects an argument in JPQL, a query language derived from HQL, to the point where Hibernate's JPQL implementation uses the org.hibernate.hql classes.

That doesn't look like valid SQL either, though. new map(...) ? You can't mix JPQL/HQL, and I haven't seen new in an SQL dialect.

I changed from

  select new map(avg(cast (speed as double precision)) as avg)
  from table 

to

      select new map(avg(cast (speed as double)) as avg)
      from table 

Hibernate supports that. I am still running as HQL query and it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM