Hibernate version:2.1
Mapping documents:
City.hbm.xml
<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.test.yellowPages.model.impl.CityImpl" table="CITY">
<id name="cityId">
<column name="ID_CITY" />
<generator class="increment" />
</id>
<property name="cityName">
<column name="NAME" />
</property>
<many-to-one name="country"
column="ID_COUNTRY"
class="com.test.yellowPages.model.impl.CountryImpl"
not-null="true" />
</class>
</hibernate-mapping>
Country.hbm.xml
<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.test.yellowPages.model.impl.CountryImpl" table="COUNTRY">
<id name="countryId">
<column name="ID_COUNTRY" />
<generator class="increment" />
</id>
<property name="countryName">
<column name="NAME" />
</property>
<set name="cities"
inverse="true"
cascade="save-update" >
<key column="ID_COUNTRY" />
<one-to-many class="com.test.yellowPages.model.impl.CityImpl" />
</set>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Session session = mSessionFactory.openSession();
Transaction tx = session.beginTransaction();
CountryImpl ci = new CountryImpl();
ci.setCountryName("China");
City ci1 = new CityImpl();
ci1.setCityName("Beijing");
ci.setCity(ci1);
City ci2 = new CityImpl();
ci2.setCityName("Shanghai");
ci.setCity(ci2);
session.save(ci);
tx.commit();
session.close();
Name and version of the database you are using:Oracle 9
I have the following problem. I would like to generate a Country and add some cities to the country. But i can't store it cascaded. Can anybodz tell my why ?? Is it possible that i have some problems with the keys ??
The code of my CityImpl class looks that:
Code:
public class CityImpl
implements City
{
private long mCityId;
private String mCityName;
private Country mCountry;
//Default Constructor
public CityImpl()
{
}
public CityImpl(String aCityName,
Country aCountry)
{
mCityName = aCityName;
mCountry = aCountry;
}
public CityImpl(long aCityId,
String aCityName,
Country aCountry)
{
this(aCityName, aCountry);
mCityId = aCityId;
}
public void setCityId(long aCityId)
{
mCityId = aCityId;
}
public long getCityId()
{
return mCityId;
}
public void setCityName(String aCityName)
{
mCityName = aCityName;
}
public String getCityName()
{
return mCityName;
}
public void setCountry(Country aCountry)
{
mCountry = aCountry;
}
public Country getCountry()
{
return mCountry;
}
}
The code of my CountryImpl Class looks that:
Code:
public class CountryImpl
implements Country
{
private long mCountryId;
private String mCountryName;
private Set mCities = new HashSet();
private static Logger mLogger =
Logger.getLogger("com.test.yellowPages.model.impl.CountryImpl");
public CountryImpl()
{
}
public CountryImpl(long aCountryId,
String aCountryName)
{
mCountryId = aCountryId;
mCountryName = aCountryName;
}
public void setCountryId(long aCountryId)
{
mCountryId = aCountryId;
}
public long getCountryId()
{
return mCountryId;
}
public void setCountryName(String aCountryName)
{
mCountryName = aCountryName;
}
public String getCountryName()
{
return mCountryName;
}
public void setCities(Set aCities)
{
mCities = aCities;
}
public Set getCities()
{
return mCities;
}
public void setCity(City aCity)
{
mLogger.debug("City added -> name = "+aCity.getCityName());
aCity.setCountry(this);
mCities.add(aCity);
}
public int getNumberOfCities()
{
return mCities.size();
}
}
Thanks for your help !!