Hello all,
I am new in hibernate and have simple question.
I have the following data model.
class Address {
int id;
Country country;
}
class Country {
int id;
String name;
}
.hbm is the following :
<class name="Country" table="Countries">
<id name="id" column="id" unsaved-value="null">
<generator class="increment"/>
</id>
<property name="name" column="name" not-null="true"/>
</class>
<class name="Address" table="Addresses">
<id name="id" column="id" unsaved-value="null">
<generator class="increment"/>
</id>
<one-to-one name="country" class="Country"/>
</class>
I load this class from database. All is OK. Then I try to modify address.
I set new country but do not change country filed just country.id. Then
I try to save modified object. There are not exceptions but my changes are not saved and just ignored.
..
int id = 1;
Address address = getAddress(id);
System.out.println(address.country.id); // pring "1"
System.out.println(address.country.name); //print "USA"
address.country.id = 2;
save(address);
...
address = getAddress(id);
System.out.println(address.country.id); // pring "1"
System.out.println(address.country.name); //print "USA"
...
I really do not want to do something like this to solve this problem :
...
Address address = getAddress(id);
address.country = getCountry(2);
save(address);
...
Could you please help me? Is is possible to automatically change country.name filed if country.id is changed?
Thanks,
Aritomo
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
Mapping documents:
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
Name and version of the database you are using:
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
Problems with Session and transaction handling?
Read this:
http://hibernate.org/42.html