These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: many-to-one joined produce inserts while selecting an entity
PostPosted: Mon Apr 19, 2010 12:23 pm 
Beginner
Beginner

Joined: Fri May 23, 2008 4:37 am
Posts: 25
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.