2

I have a dynamic search with 25 optional parameters. HQl is not an option. I am using the Criterion API. I have DAO methods that I would like to accept a 'Restriction' List that I can build in my service layer such that, when I call my DAO method from the service as follows:

Lits<myPojoClass> = myDAO.getDataByCriterion( <?Restriction List?> )

myDAO.getDataByCriterion could consume the 'Restriction List' as follows:

inside the myDAO class

Public List<myPojoClass> getDataByCriterion( <?Restriction List?> restrictionList) {
    Session s = HibernateUtil.currentSession();
    Criteria c = s.createCriteria(myPojo.class)
                  .add(Restrictions ( <?Restriction List?>  );  //attach the list here
    List<myPojoClass> response = c.list();

It seems like somthing you should be able to do. Is it possible to add the restrictions passed into the DAO method

hammar
  • 138,522
  • 17
  • 304
  • 385
JJRutter
  • 85
  • 3
  • 14
  • Declaring single argument of type `Criterion` possibly is better idea because this way you always have determined criterion. Having list of them you should consider about default logic operation for interpreting them. Possibly it would be a conjunction but first way is more flexible. On other hand I'd avoid this kind of design solution cause it breaks out incapsulation for DAO layer. You will require hibernate dependancies. I'd suggest to keep creating domain oriented methods for each specific case with simple arguments. – Viktor Stolbin Jul 17 '12 at 15:37

1 Answers1

3

Yes, it's possible. Your Restriction List should be a regular List of Criterion objects (i.e., List <Criterion>). Just to be clear, if you add them all like your sample code, you're gonna be doing a conjunction (a series of logical ANDs).

Adding my suggestion to your code gives this:

Public List<myPojoClass> getDataByCriterion( List<Criterion> restrictionList) {
Session s = HibernateUtil.currentSession();
Criteria c = s.createCriteria(myPojo.class);
for (Criterion crit : restrictionList){
    c.add(crit);
}

List<myPojoClass> response = c.list();
Andre
  • 3,874
  • 3
  • 35
  • 50
  • Thanks, this sounds like what I need. But, how do I actually create the Criterion objects. – JJRutter Jul 17 '12 at 16:33
  • @JJRutter Static methods in org.hibernate.criterion.Restrictions return many different kind of implementations of Criterion. – Mikko Maunu Jul 17 '12 at 16:48
  • They've got all sorts of Criterion implementations... take a look at the available ones, they should be enough for most cases. In cases you need to implement your own, it's just a matter of implementing the Criterion interface. I did that in the past when I needed to do a text search on date fields; here's how to do that in case you're interested: http://stackoverflow.com/a/10799592/323807 – Andre Jul 17 '12 at 18:12
  • Yes there's all types of criterion implementations. However, i'd stick the Restrictions class factory methods unless you absolute have to do something it can't do. – Matt Jul 17 '12 at 21:38
  • @Matt I actually meant that the Restriction factory has all sorts of implementations; those are tested and AFAIK work on most engines. So yeah, you should do your best to stick to those. But I just wanted to point out that you shouldn't feel restricted to the implementation in the Restrictions factory. In my case, for instance, I needed a text search on a field date. You could do that with an sql restriction from the Restriction factory, but you would be vulnerable to SQL injection. – Andre Jul 18 '12 at 12:24