-->
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.  [ 5 posts ] 
Author Message
 Post subject: How use criteria correctly?
PostPosted: Wed Jan 28, 2004 5:27 am 
Newbie

Joined: Tue Jan 27, 2004 7:48 am
Posts: 1
Good time of day!

I have follow question.

I have city, building and buildingOwner persistance objects in my application and I wish to select all buildings with city where building.buildingOwner.name = "BuildingOwnerName" and sorted by city.name.

city have one-to-many relation to building and
buildingOwner have one-to-many relation to building

When I using HQL:

select distinct building, city
from
city in class com.myapp.City,
building in city.buildings.elements,
where
building.buildingOwner.name = "BuildingOwnerName"
order by
city.name

How can do it with using Criteria?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2004 8:27 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
I'm not sure how to return multiple objects in a criteria query (or even if it is possible), but you could try something like:
Code:
Criteria crit = session.createCriteria(City.class)
    .createCriteria("buildings")
    .add(Expression.eq("buildingOwner.name", name)

or
Code:
Criteria crit = session.createCriteria(City.class)
    .createCriteria("buildings")
    .createCriteria("buildingOwner)
    .add(Expression.eq("name", name)



I haven't tried multiple join paths in an Expression either (first example) but it may work....

This should return a list of Cities objects and you can throw out duplicates/navigate the tree.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2004 10:28 am 
Beginner
Beginner

Joined: Tue Nov 11, 2003 4:49 am
Posts: 47
Location: Florence, Italy
This is a piece of code I use that do what you asked.
Ciao.

Code:
   
   public List ricercaMalattia(String nome, String cognome, Date inizio, Date fine)
   {
      try
      {
         Criteria ricercaMalattie = HibernateSession.getSession().createCriteria(MalattiaImpl.class);
         
         if (inizio != null)
         {
            ricercaMalattie.add(Expression.gt("inizio", inizio));
         }

         if (fine != null)
         {
            ricercaMalattie.add(Expression.lt("fine", fine));
         }

         ricercaMalattie = ricercaMalattie.createCriteria("dipendente");
         
         if (StringUtils.isNotBlank(nome))
         {
            ricercaMalattie = ricercaMalattie.add(Expression.like("nomeProprio", nome+"%"));
         }

         if (StringUtils.isNotBlank(cognome))
         {
            ricercaMalattie = ricercaMalattie.add(Expression.like("cognome", cognome+"%"));
         }
         
         return ricercaMalattie.list();
      }
      catch (HibernateException e)
      {
         return Collections.EMPTY_LIST;
      }
   }


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 29, 2004 12:55 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Instead of
Code:
ricercaMalattie = ricercaMalattie.add(Expression.like("nomeProprio", nome+"%"));

You can use:
Code:
ricercaMalattie = ricercaMalattie.add(Expression.like("nomeProprio", nome, MatchMode.START));


Also - beware of not propagating or logging your exceptions.
I would (at least):
Code:
catch (HibernateException e)
      {
         log.warn(e.getMessage()); // log being an org.apache.commons.logging.Log class
         return Collections.EMPTY_LIST;
      }
   }


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 29, 2004 4:15 am 
Beginner
Beginner

Joined: Tue Nov 11, 2003 4:49 am
Posts: 47
Location: Florence, Italy
Thank you for your advice about criteria, It's yet in my code ;-)
The missing log was a fault of that piece of code only.
Ciao.


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