1

I'm using the hibernate generic dao project. I need to search for all records where the 'date1' field is earlier than 'date2', something like:

search.addFilterLessThan('date1', 'date2');

However it seems from the API that the search only accepts values as the second argument and not properties:

Date date = ...;
search.addFilterLessThan('date1', date);

Is there a way to filter based on two record fields?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Alex
  • 1,041
  • 3
  • 14
  • 32

2 Answers2

1

I assume you are using the Hibernate criteria API. If you are, comparing properties is easy:

Criteria criteria = session.createCriteria(SomeObject.class);
criteria.add(Property.forName("date1").ltProperty("date2"));
criteria.list();

You can also compare across joins and a variety of other scenarios supported by SQL. This sort of thing is available via HQL as well.

Update after reviewing Hibernate Generic DAO Project:

Looks like the Hibernate Generic DAO project ties your hands in terms of hibernate features, limiting the variety of queries possible (not exposing Criteria or HQL queries!). I do not see this as value added, even with the claim that it "Simplifies" your queries or makes them more "Robust".

John Ericksen
  • 10,995
  • 4
  • 45
  • 75
  • I'm using the Search and Filter classes of hibernate-generic-dao, would prefer to use them for consistency with the rest of the project. If I can't achieve what I need with those classes I'll use criteria – Alex Feb 24 '12 at 19:47
  • Wow, that framework really ties your hands, and not in a good way. Looks like it is not possible to do this type of search using the wrapping "hibernate-generic-dao" project with its Search or Filter classes. I would rip it out and either write your own DAOs or create a better, more flexible / less restricting 'generic dao' for your own project. – John Ericksen Feb 25 '12 at 00:15
  • Additionally, that particular project is no longer supported by the original developer. – John Ericksen Feb 25 '12 at 00:15
  • At my work we created a similar 'Generic DAO' implementation but we made sure to allow any calling code to access the hibernate Session, allowing the developer to create any sort of query imaginable including HQL and Criteria queries. – John Ericksen Feb 25 '12 at 00:16
  • Seeing the project is no longer supported I guess we'll have to do that as well when there's more time available. Perhaps extending the framework to allow for criteria searches will work in the short term – Alex Feb 25 '12 at 05:46
1

Got an answer from the project's group, it's done using custom filters:

search.addFilterCustom("{date1} < {date2}"); 
Alex
  • 1,041
  • 3
  • 14
  • 32