0

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?

3
  • That looks like (mostly) SQL, not HQL. If you want to use Hibernate, you need to use the Hibernate query language, which has different type names, etc. Commented Jul 17, 2013 at 8:30
  • I am using Native SQL in Hibernate. Commented Jul 17, 2013 at 8:30
  • 2
    then you need to use createNativeQuery Commented Jul 17, 2013 at 8:31

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.