-->
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.  [ 3 posts ] 
Author Message
 Post subject: one-to-many mapping
PostPosted: Mon Apr 25, 2005 2:56 am 
Newbie

Joined: Mon Apr 25, 2005 2:37 am
Posts: 5
Hi,
I have two classes;

public class City {
private int id;
private String name;
private String plate;
private List provinces;
//GETTERS
//SETTERS
}

public class Province {
private int id;
private City city;
private String name;
//GETTERS
//SETTERS
}

and my mapping file is here;

<class name="City" table="CITY">
<id name="id" column="ID" type="int">
<generator class="native"/>
</id>
<property name="name" column="NAME"/>
<property name="plate" column="PLATE"/>
<bag name="provinces">
<key column="ID"/>
<one-to-many class="Province"/>
</bag>
</class>

<class name="Province" table="PROVINCE">
<id name="id" column="ID" type="int">
<generator class="native"/>
</id>
<property name="name" column="NAME"/>
<many-to-one name="city" column="CITYID"/>
</class>

i use this code fragment to retrieve a city and all related provinces;

public City getCityAndAllProvinces(int cityid) throws HibernateException {
Session s = sessions.openSession();
Transaction tx = null;
City city = null;
try {
tx = s.beginTransaction();
Query q = s.createQuery("from City as city left outer join fetch city.provinces " +
"where city.id = :cityid ");
q.setParameter("cityid", new Integer(cityid));
city = (City) q.list().get(0);
}
catch (HibernateException e) {
if (tx != null) {
tx.rollback();
}
throw e;
}
finally {
s.close();
}
return city;
}

with this code i retrieve only first province related to the city. But i want to retrieve all of them.

AdminHelper helper = new AdminHelper();
City city = null;
try {
city = helper.getCityAndAllProvinces( 0 );
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (HibernateException e) {
e.printStackTrace();
}
Iterator iter = city.getProvinces().iterator();
log(city.getName() + "'in ilçeleri: ");
while (iter.hasNext()) {
Province province = (Province) iter.next();
log("-->" + province.getName());
}

how can i do this?
Thanks for your interest...


Top
 Profile  
 
 Post subject: One-to--Many
PostPosted: Mon Apr 25, 2005 4:51 am 
Beginner
Beginner

Joined: Wed Apr 21, 2004 8:33 am
Posts: 27
When you have define the mapping as one-to-many trhough set or Bag ,
Say in each city can have many provinces if i am not wrong.
So have an one-to-many mapping with set from city to Province. In your City Class(value Object) file define Set provinces = new HastSet();
Now when you quiery on the city table for a particualr city Id you will get the City. Now You can Iterate through the set of Provinces for that particualr city and get all the provinces.

Like :

Criteria crit = Criterai.add(City.Class)
.expression.eq("CityID",cityId);
City cityResult = crit.uniqueResult();

Set provincesSet = cityResult .getProvinces();
Iterate provincesSetIter = provincesSet .iterate();
while(provincesSetIter .hasNext()){\

Provinces provinceClass = (Province)provincesSetIter .hasNext();
//from here u get all the provinces and u can associate each province to different models or whatever.



}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 25, 2005 6:36 am 
Newbie

Joined: Mon Apr 25, 2005 2:37 am
Posts: 5
Thanks for your interest yvijaykumar,

i have solved my problem,

<class name="City" table="CITY">
<id name="id" column="ID" type="int">
<generator class="native"/>
</id>
<property name="name" column="NAME"/>
<property name="plate" column="PLATE"/>
<bag name="provinces">
<key column="CITYID"/>
<one-to-many class="Province"/>
</bag>
</class>

it's only my mistake!


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