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...
|