-->
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.  [ 2 posts ] 
Author Message
 Post subject: using criteria query
PostPosted: Wed Dec 17, 2008 12:21 pm 
Newbie

Joined: Wed Dec 17, 2008 12:17 pm
Posts: 11
I have this method in my AbstractHibernateDAO

Code:
    @SuppressWarnings("unchecked")
    protected List<T> findByCriteria(Criterion... criterion) {
        Criteria crit = getSession().createCriteria(getPersistentClass());
        for (Criterion c : criterion) {
            crit.add(c);
        }
        return crit.list();
   }
   


in sub classes I am calling using the methods like this

Code:
   public List<StgAuditGeneral> findBySubAgencyCode(Long subAgencyCode) {
      return findByCriteria(Restrictions.eq("subAgencyCode", subAgencyCode));
   }


now I am not clear ion sub class how I can add multiple criteria and Also I want to setMaxResults, setFirstResult and add orderBy , please help me how I can do this ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2008 9:04 pm 
Newbie

Joined: Wed Dec 17, 2008 6:35 pm
Posts: 11
That method takes Criterion varargs so to add multiple you just pass in a bunch of Restrictions comma delimited (is that the first part of the question?).

The second part it looks like you want to set order, maxresults etc. You could add a new method to your AbstractHibernateDAO:

Code:
protected List<T> findByCriteria(DetachedCriteria criteria) {
        return criteria.getExecutableCriteria(getSession()).list();
   }


then your other method would look something like this

Code:
public List<StgAuditGeneral> findBySubAgencyCode(Long subAgencyCode) {
   DetachedCriteria detachedCriteria = DetachedCriteria.forClass(StgAuditGeneral.class);
   detachedCriteria.add(Restrictions.eq("subAgencyCode", subAgencyCode));
   //add more restrictions here if you need.
   detachedCriteria.addOrder(Order.asc("subAgencyCode"))
   return findByCriteria(detachedCriteria);
   }


The problem with this is that maxResults can't be set on a DetachedCriteria object but rather a Criteria object (which you have once you call criteria.getExecutableCriteria(getSession())). So you could pass the max results into the method (dirty) or just use a criteria object and not worry too much about using your method in AbstractHibernateDAO.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.