Hi everyone,
I have a problem with a many-to-one relationship. In fact my mapping works but I don't undesrtand what Hibernate is doing.
I have "formation" object, this object can be associated with a "section" and this association contains an attribute "year".
Code:
CREATE TABLE formations_section (
fu_id INTEGER NOT NULL,
sec_id INTEGER NOT_NULL,
year INTEGER NOT NULL
);
My mapping is :
Code:
<hibernate-mapping>
<class name="xx.yy.Formation" table="formationsunits">
<id column="fu_id" name="id">
<generator class="sequence">
<param name="sequence">formationsunits_fu_id_seq</param>
</generator>
</id>
<property column="fu_code" name="code" />
<property column="fu_name" name="name" />
<property column="fu_description" name="description" />
<join table="formationsunits_section" inverse="false" optional="true">
<key column="fu_id" />
<many-to-one name="section" column="sec_id" not-null="false" lazy="false"/>
</join>
<join table="formationsunits_section" inverse="false" optional="true">
<key column="fu_id" />
<property name="year" column="year" lazy="false" not-null="false"/>
</join>
</class>
</hibernate-mapping>
When I try to retrieve a "formation" I can see that Hibernate is doing some inserts and these inserts fails when the formation isn't associated with a section (that is not mandatory) :
Quote:
Hibernate:
/* load one-to-many xx.yy.Formation.declinaisons */ select
declinaiso0_.fu_decline as fu5_1_,
declinaiso0_.fu_id as fu1_1_,
declinaiso0_.fu_id as fu1_54_0_,
declinaiso0_.fu_code as fu2_54_0_,
declinaiso0_.fu_name as fu3_54_0_,
declinaiso0_.fu_description as fu4_54_0_,
declinaiso0_.fu_decline as fu5_54_0_,
declinaiso0_1_.sec_id as sec2_55_0_,
declinaiso0_2_.year as year55_0_
from
formationsunits declinaiso0_
left outer join
formationsunits_section declinaiso0_1_
on declinaiso0_.fu_id=declinaiso0_1_.fu_id
left outer join
formationsunits_section declinaiso0_2_
on declinaiso0_.fu_id=declinaiso0_2_.fu_id
where
declinaiso0_.fu_decline=?
Hibernate:
/* insert xx.yy.Formation
*/ insert
into
formationsunits_section
(year, fu_id)
values
(?, ?)
Hibernate:
/* insert xx.yy.Formation
*/ insert
into
formationsunits_section
(sec_id, fu_id)
values
(?, ?)
Hibernate:
/* insert xx.yy.Formation
*/ insert
into
formationsunits_section
(year, fu_id)
values
(?, ?)
Of course I have updated the mapping to avoid the error on null but I don't want to do inserts when retrieving a formation.
Can you, plese, help me to understand what I'm doing wrong and solve that ?
Thanks