Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CriteriaAPI -how to bin Expressions like hour(...)

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I use the hql-expressions (Hibernate Reference 14.10. Expressions) in a Criteria-Instance?

I tried something like:



but it does not work as hibernate cannot resolve the property year(myDate).



In HQL, it is legitim to say:



Any Ideas?
 
Angel Taveras
Ranch Hand
Posts: 84
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello you could add a sqlRestriction to it. org.hibernate.criterion.Restrictions.sqlRestriction(String, Object, Type),

regards
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey,


using sqlRestriction makes it database-dependent, am I right?

However, the Expressions are all available in HQL.

How can I achieve a database-independent-solution? There is nothing something like hqlRestriction!?

I have this which does not work:

.add(Restrictions.between("month(myDate)", fromMonth, toMonth))

Should I do something like this:



This does not work.
 
Angel Taveras
Ranch Hand
Posts: 84
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code could be



but you need and extra "property" for use between what i recommend it's to write your own expression it's simple and easy. But i agree with
you that this need to be done in some other way.

regards,
 
Ram kovis
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can get year from myDate using Date utils and pass that value to restrictions..
 
Angel Taveras
Ranch Hand
Posts: 84
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He could do that Ram, but he have to load the object first, then with a second query do the rest.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay,

got it:



I guess, it is database-independent, as between..of... is SQL-Syntax for all sql-databases!
Am I right?

thanks.
 
Angel Taveras
Ranch Hand
Posts: 84
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
between it's a functionality of the standard sql but the year function that you're using not sure about it's portability,
but i hope that it worked for you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic