This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes CriteriaAPI -how to bin Expressions like hour(...) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "CriteriaAPI -how to bin Expressions like hour(...)" Watch "CriteriaAPI -how to bin Expressions like hour(...)" New topic
Author

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

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
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

Joined: Nov 13, 2008
Posts: 84

Hello you could add a sqlRestriction to it. org.hibernate.criterion.Restrictions.sqlRestriction(String, Object, Type),

regards
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
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

Joined: Nov 13, 2008
Posts: 84

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

Joined: Jun 23, 2005
Posts: 130
you can get year from myDate using Date utils and pass that value to restrictions..
Angel Taveras
Ranch Hand

Joined: Nov 13, 2008
Posts: 84

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

Joined: Jun 28, 2008
Posts: 580
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

Joined: Nov 13, 2008
Posts: 84

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
 
Consider Paul's rocket mass heater.
 
subject: CriteriaAPI -how to bin Expressions like hour(...)
 
Similar Threads
NullPointerException in JSP after calling Servlet method
Property named id but not primary key
Need Help Converting from HQL to Criteria
Parent and subcollection fetching
What is object?