-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Hibernate HQL query does not support PostgreSQL "::" casting
PostPosted: Thu Sep 21, 2017 5:51 pm 
Newbie

Joined: Fri Sep 01, 2017 2:40 am
Posts: 17
Hi, I'm trying to run the query below in postgres with em.createQuery();

Code:
String q = "select id from Calendar c " +
    "where c.isActive = 1 and " +
    "date_part('dow', '2017-09-19 13:23:23'::date) = c.frequencyValue)";
Query query = em.createQuery(q);
List results = query.getResultList();


The problem I have now is if I don't include
Code:
::date
, postgres will throw an exception.
Code:
Could not choose a best candidate function. You might need to add explicit type casts.

but If I include ::date, hibernate will complain.


Top
 Profile  
 
 Post subject: Re: cant have ::date in query
PostPosted: Fri Sep 22, 2017 1:53 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
The :: PostgreSQL operator will not be recognized by the JPQL query parser so Hibernate will complain.

What you need here is to use TO_DATE or TO_TIMESTAMP to convert the String date/time to a Date or Timestamp.

So, try it like this:

Code:
String q = "select id from Calendar c " +
    "where c.isActive = 1 and " +
    "date_part('dow', TO_TIMESTAMP('2017-09-19 13:23:23', 'YYYY-MM-dd H24:MI:SS')) = c.frequencyValue)";
Query query = em.createQuery(q);
List results = query.getResultList();


Top
 Profile  
 
 Post subject: Re: cant have ::date in query
PostPosted: Fri Sep 22, 2017 11:53 am 
Newbie

Joined: Fri Sep 01, 2017 2:40 am
Posts: 17
vlad wrote:
The :: PostgreSQL operator will not be recognized by the JPQL query parser so Hibernate will complain.

What you need here is to use TO_DATE or TO_TIMESTAMP to convert the String date/time to a Date or Timestamp.

So, try it like this:

Code:
String q = "select id from Calendar c " +
    "where c.isActive = 1 and " +
    "date_part('dow', TO_TIMESTAMP('2017-09-19 13:23:23', 'YYYY-MM-dd H24:MI:SS')) = c.frequencyValue)";
Query query = em.createQuery(q);
List results = query.getResultList();


Thanks Vald. I ended up using cast(). was trying to see if there's a way for hibernate to handle :: because it is easier to read for me. Anyway, thank you so much!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.