-->
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.  [ 7 posts ] 
Author Message
 Post subject: ORM using HQL (or SQL)
PostPosted: Sat May 06, 2006 3:49 am 
Newbie

Joined: Sat May 06, 2006 3:29 am
Posts: 4
Hibernate version: 3.2 cr1

I have 3 tables COUNTRY, REGION and CITY. The structure like this:

Code:
COUNTRY
ID, NAME, {Other Data}

REGION
ID, CID, NAME, {Other Data}

CITY
ID, RID, NAME, {Other Data}

COUNTRY <-----> REGION <------> CITY
        1     n        1     n



There is no problem with the orm for the above in Hibernate. Now, I want to have a property "cities" under class "Country" which is a collection of the cities in a country, however, I have no idea to do mapping using HQL like this:

Code:
from City c where c.rid in (select r.id from Region r where r.cid = {country id}



Can anyone help with this? Thanks in advance.

Eric


Top
 Profile  
 
 Post subject: Re: ORM using HQL (or SQL)
PostPosted: Sat May 06, 2006 4:25 pm 
Beginner
Beginner

Joined: Tue May 02, 2006 10:04 am
Posts: 29
Do you want mapping (with hbm files) or the correct query (with HQL)?


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 07, 2006 12:32 am 
Newbie

Joined: Sat May 06, 2006 3:29 am
Posts: 4
Hi imchi,

Sorry for ambiguity. I have no problem to do query with HQL, in fact, I can even write Java code in the class to do the same thing, but I want to have cleaner (clear) code, so I want to have mapping in hbm file, if Hibernate is capable to do this.

Thanks again with your help.

Eric


Top
 Profile  
 
 Post subject: Re: ORM using HQL (or SQL)
PostPosted: Sun May 07, 2006 9:02 am 
Beginner
Beginner

Joined: Sun Dec 21, 2003 11:19 pm
Posts: 22
Location: Kentucky, USA
Eric,
It seems to me that this is a Java class question not a Hibernate question. I would use a derived property in the Country class to return all the Cities. This assumes that a City has to have a Region.
Here is what I would do in Java.

Code:
public class Country {
   private Long ID;
   private Set<Region> regions;
   public  Long getID() {
      return this.ID;
   }
   public  void setID(Long id) {
      this.ID = id;
   }
   public  Set<Region> getRegions() {
      return this.regions;
   }
   public  void setRegions(Set<Region> regions) {
      this.regions = regions;
   }
   public Set<City> getCities() {
      Set<City> cities = new HashSet<City>();
      for (Iterator iter = regions.iterator(); iter.hasNext();) {
         Region element = (Region) iter.next();
         cities.addAll(element.getCities());
      }
      return cities;
   }
}

public class Region {
   private Long ID;
   private Country country;
   private Set<City> cities;
   
   public Set<City> getCities() {
      return this.cities;
   }
   public void setCities(Set<City> cities) {
      this.cities = cities;
   }
   public Country getCountry() {
      return this.country;
   }
   public void setCountry(Country country) {
      this.country = country;
   }
   public Long getID() {
      return this.ID;
   }
   public void setID(Long id) {
      this.ID = id;
   }
}



The only Hibernate concern is to be sure that for the requested Country all the Region(s) and City(s) have been returned. One approach would be something like this in a session service object
Code:
public Country loadCitiesForCountry(Long countryID) {
   Session session = openSession();
   Criteria loadQuery = session.createCriteria(Country.class);
   loadQuery.add(Restrictions.idEq(id));
   loadQuery.setFetchMode("regions", FetchMode.JOIN);
   loadQuery.setFetchMode("regions.cities", FetchMode.JOIN);
      
   Country c = (Country) loadQuery.uniqueResult();
   session.close();
   return c;
}


Hope this helps.
Timothy Vogel


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 07, 2006 12:36 pm 
Newbie

Joined: Sat May 06, 2006 3:29 am
Posts: 4
Hi Timothy,

Thanks for your advice. It is what I am coding.

Nevertheless, I want to know if Hibernate is capable to having mapping like this. Though, in this case, it is trivial to write Java code as alternative, some queries can be represented easily by HQL (or SQL) while it is difficult and inefficient to return the same result using Java code. I hope that Hiberate can do this.

But anyway, your effort is much appreciated.

Eric


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 07, 2006 3:26 pm 
Beginner
Beginner

Joined: Tue May 02, 2006 10:04 am
Posts: 29
http://www.onjava.com/pub/a/onjava/2005/08/03/hibernate.html - maybe this could help you.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 10:35 am 
Newbie

Joined: Sat May 06, 2006 3:29 am
Posts: 4
Hi imchi,

Great! Your resource is helpful. As I read in the article, formula provides only scaler or object property mapping. Collection property is not supported.

Thanks,
Eric


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