Hi I have a question about object identities and cascading saves.
As an example assume I have two classes:
Language which has two fields 'code' for example 'en' and 'name' for example 'English'
Code:
<hibernate-mapping package="domain">
<class name="Language" table="pb_language">
<id name="code" column="code" type="string" length="10">
<generator class="assigned"/>
</id>
<property name="name" type="string" not-null="true" length="100"/>
</class>
</hibernate-mapping>
Person which has a number of fields 2 related to Language, 'defaultLanguage' and
'languages'.
Code:
<hibernate-mapping package="domain">
<class name="Person" table="pb_person">
<id name="id" column="id" type="long">
<generator class="native"/>
</id>
<property name="name" type="string" not-null="true" length="100"/>
<many-to-one name="defaultLanguage" class="Language" not-null="true" cascade="save-update" />
<set name="languages" table="pb_person_languages" cascade="save-update">
<key column="person_id" />
<many-to-many column="language_id" class="Language" />
</set>
</class>
</hibernate-mapping>
My application constructs a Person class with the associated Language objects. But I am having 2 problems:
1- if I specify that defaultLanguage is not-null then adding the person class fails with:
not-null property references a null or transient value: domain.Person.defaultLanguage
Note, that Person does have a defaultLanguage (ie it is not null).
2- if the default language is also in the languages set, adding fails telling me:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [domain.Language#en]
Perhaps I need to change my PersonDAO class to make sure it creates Language objects prior to trying to save a Person,
but I thought that was the use of cascade.
Perhaps I have messed up with identifiers, although I did override the equals, and hashcode methods of Language.
Most likely I have misunderstood something. I am hoping someone in this forum has gone through the same problem and
can offer me some advice.
Thanks,
Hani