-->
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: hql to criteria
PostPosted: Thu Jan 18, 2007 9:36 am 
Beginner
Beginner

Joined: Sat Feb 18, 2006 6:24 am
Posts: 23
I am trying to translate this hql query:
Code:
select distinct ci.savingType from CalculationItem as ci where ci.currency=?


into Criteria API query.
So far , this is the best that I can do:

Code:
Criteria cr = sessionFactory.getCurrentSession().createCriteria(CalculationItem.class);
      if(currency!=null)
      {
         cr.add(Restrictions.eq("currency", currency));
      }
      
      cr= cr.createCriteria("savingType", "st").setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
   
       List<Map> firstList = cr.list();
       List<SavingType> list= new ArrayList<SavingType>();
       for (Map<String, SavingType> type : firstList)
      {
          SavingType one = type.get("st");
          if(!list.contains(one))
         {
             list.add(one);
         }
      }
      return list;


Is there any better way to to this. I would like to avoid "manual" filtering of duplicate SavingType objects.

Thanx!


Top
 Profile  
 
 Post subject: Solved it! :-)
PostPosted: Thu Jan 18, 2007 10:02 am 
Beginner
Beginner

Joined: Sat Feb 18, 2006 6:24 am
Posts: 23
Solution is pretty simple:

Code:
Criteria cr = sessionFactory.getCurrentSession().createCriteria(CalculationItem.class);
      if(currency!=null)
      {
         cr.add(Restrictions.eq("currency", currency));
      }
       
      cr = cr.setProjection(Projections.distinct(Projections.property("savingType")));

return cr.list();


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.