Hi
I have some problems with my many-to-many association.
I'm using Hibebernate 3 and Spring 2.
I have a table person, a table language and a table persons_languages
In my java object, I have a class Person which has a set of Language and a class Language which has a set of Person.
here are the mapping files:
Language.hbm.xml
Code:
<hibernate-mapping>
<class name="com.unisys.r4egov.eaw.spring.bo.Languages" table="languages" catalog="eaw1">
<comment></comment>
<id name="id" type="string">
<column name="ID" length="3" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="NAME" length="20">
<comment></comment>
</column>
</property>
<bag name="reqPersonsAs" table="persons_languages" outer-join="true" cascade="all" lazy="false">
<key>
<column name="LANGUAGE_ID" length="3" not-null="true">
<comment></comment>
</column>
</key>
<many-to-many entity-name="com.unisys.r4egov.eaw.spring.bo.ReqPersonsA">
<column name="PERSON_ID" not-null="true">
<comment></comment>
</column>
</many-to-many>
</bag> </class>
</hibernate-mapping>
person.hbm.xml
Code:
<hibernate-mapping>
<class name="com.unisys.r4egov.eaw.spring.bo.ReqPersonsA" table="req_persons_a" catalog="eaw1">
<comment></comment>
<id name="id" type="int" unsaved-value="0">
<column name="ID" />
<generator class="identity" />
</id>
<property name="name" type="string">
<column name="NAME" length="200">
<comment></comment>
</column>
</property><bag name="languageses" outer-join="true" inverse="true" table="persons_languages" cascade="all" lazy="false" >
<key>
<column name="PERSON_ID" not-null="true" >
<comment></comment>
</column>
</key>
<many-to-many entity-name="com.unisys.r4egov.eaw.spring.bo.Languages" >
<column name="LANGUAGE_ID" length="3" not-null="true" >
<comment></comment>
</column>
</many-to-many>
</bag>
</class>
</hibernate-mapping>
When I insert a Person which has the language 'fr', it is correctly inserted in the table persons_languages. But if i had a second person which has also the language 'fr', hibernate delete from the table persons_languages, the first person with the language 'fr' and insert the second person with the language 'fr'.
Here are the query sql generated by hibernate:
Code:
Hibernate: insert into eaw1.req_persons_a (NAME) values (?)
Hibernate: select languages_.ID, languages_.NAME as NAME12_ from eaw1.languages languages_ where languages_.ID=?
Hibernate: insert into eaw1.eu_arrest_warrant (CENTRAL_ID, SIGNATURE_ID, PERSON_ID, DECISION_ID, SENTENCE_ID, LANGUAGE_ID, ISSUING_JUDICIAL_AUTHORITY_ID, ABSENTIA_ID, DATE, LEGAL_GARANTEES_D, OTHER_CIRCUMSTANCES_F, TOTAL_OFFENCES_E, FULL_DESCRIPTION_OFFENCES_E) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: delete from persons_languages where LANGUAGE_ID=?
Hibernate: insert into persons_languages (LANGUAGE_ID, PERSON_ID) values (?, ?)
Why does hibernate delete all the entries with 'fr' before insert the new entry in persons_languages??