nordborg wrote:
Code:
private Country countryObject;
public Country getCountryObject()
{
return countryObject;
}
public void setCountryObject(Country countryObject)
{
this.countryObject = countryObject;
}
You may also want to make sure the object and string representation of the country remains synchronized when one of the setCountry() or setCountryObject() methods are called.
In your example, is countryObject a new object a side from id and country?
I quess this is used to make the relation to the Country class.
I used country as Foreign Key, will countryObject replace this and make a FK in de db, or is it just for the relation in java.
Edit:
I gave city a countryFK (Country) variable with getter and setter and the error was gone.
Do I still need the country (String) variable or can hibernate handle this and make the FK?
Edit 2:
I tested it in one class and it works.
Now I have the following. If anyone sees something I can improve, please tell me?
My mapping is now:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="data.City" table="cities">
<id name="id" column="id" length="20">
<generator class="assigned" />
</id>
<many-to-one name="country" class="data.Country" column="country"
insert="false" update="false" />
</class>
</hibernate-mapping>
And the java class:
Code:
package data;
import hibernate.IdCounter;
public class City
{
private String id;
private Country country;
public City(String country)
{
setId(id = IdCounter.getInstance().getCityId());
}
public City()
{
}
public String getId()
{
return id;
}
public Country getCountry()
{
return country;
}
private void setId(String id)
{
this.id = id;
}
public void setCountry(Country country)
{
this.country = country;
}
}