-->
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.  [ 5 posts ] 
Author Message
 Post subject: Problems with composition and one-to-one
PostPosted: Tue Aug 23, 2005 9:39 am 
Newbie

Joined: Mon Aug 15, 2005 8:03 am
Posts: 14
Location: Germany
Hi,

I've here a problem with one-to-one mapping.
I've tow classes Instrument and Option. The Relation between them is
Code:

  +------------+          +----------+
  | Instrument |          |  Option  |
  +------------+ 1   0..1 +----------+
  | id*        |<>--------| id*      |
  | ...        |          | inst_id  |
  +------------+          +----------+


Each classe has its own table defined as follows:
Code:
create table instrument (
   id int not null auto_increment primary key,
   name varchar(30) not null
);

create table option (
   id int not null auto_increment primary key,
   inst_id int not null,

   constraint instrument_option_fk_01 foreign key (inst_id) references instrument (id)
);



The corresponig hbm file contains the definition of the classes and the one-to-one relation as follows:

Code:
<hibernate-mapping package="xxx" default-lazy="false">
   <class name="Instrument" table="instrument" lazy="false">
      <id name="id"
         column="id"
         type="int"
         unsaved-value="0"
      >
         <generator class="native"/>
      </id>
      <property
         name="Name"
         column="name"
         length="30"
         type="string"
         not-null="true"
       />
      
      <one-to-one
         name="option"
         class="Option"
         fetch="join"
         lazy="false"
        />
   </class>

   <class name="Option" table="option" lazy="false">
      <id name="id"
         column="id"
         type="int"
         unsaved-value="0"
      >
         <generator class="native"/>
      </id>
   </class>
</hibernate-mapping>


The call of my dao for loading all Instruments (my DAO is extending HibernateDaoSupport) is implemented as follows:
Code:
   public List<Instrument> getAll() {
      return getHibernateTemplate().loadAll(Instrument.class);
   }


In my data base I've three Instruments and two Options. The instrument with the id 1 is not referenced by any option. The instruments with id 2 and 3 are referenced by options with id 1 and 2.

After calling loadAll() I get all three Instruments but the option objects are not attached correctly. The first Instrument (with id 1) contains the first option and that with id 2 the another option. The third instrument doen't have any options.

It seems that the hibernate loads all instruments and all options and then starts to attach the options in the order of instruments. It doesn't take care of given inst_id.

What do I wrong.
I'm using hibernate 3.1 b2 with JDK 1.5. For tests I'm using MsSql 4.1

Thanks in advance,
Reza


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 10:29 am 
Newbie

Joined: Mon Aug 15, 2005 8:03 am
Posts: 14
Location: Germany
Hi,

I found it. The class Option needs an attribute instrument_id. That shoud be given in hbm for Instrument as property-ref.

The Problem I have now is, that in case of saving a new instrument, the corresponding Option has not been save automaticly.

Save just call:
Code:
getHibernateTemplate().saveOrUpdate(instrument);
.

How can I force hibernate to make a deep persistence?

Reza


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 12:03 pm 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
add cascade attribute to the association...

Code:
   <one-to-one
         name="option"
         class="Option"
         fetch="join"
         lazy="false"
         cascade="all"
        />


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 1:25 pm 
Newbie

Joined: Mon Aug 15, 2005 8:03 am
Posts: 14
Location: Germany
hi anar,

anar wrote:
add cascade attribute to the association...

Code:
   <one-to-one
         name="option"
         class="Option"
         fetch="join"
         lazy="false"
         cascade="all"
        />


I did it and it works perfectly.

There is still a problem. By saving an Instrument hibernate tries to save also the associated Option.
I assumed that hibernate firstly stores instrument and then sets the filed inst_id of the option to the generated id of the instrument and then stores the option.
hibernate stores the instrument but doesn't set the filed inst_id of the option and the storing fails with following message:
Code:
WARN JDBCExceptionReporter:71 - SQL Error: 1216, SQLState: 23000
ERROR JDBCExceptionReporter:72 - Cannot add or update a child row: a foreign key constraint fails


The hbn part is:
Code:
      <one-to-one
         name="option"
         class="Option"
         fetch="join"
         lazy="false"
         property-ref="instrumentId"
         cascade="all"
      />

and Option is defined as follows:
   <class name="Option" table="option" lazy="false">
      <id name="id"
         column="id"
         type="int"
         unsaved-value="0"
      >
         <generator class="native"/>
      </id>
      <property
         name="instrumentId"
         column="instrument_id"
         type="int"
       />
   </class>



How can I say hibernet to set the field inst_id to the value of instrument.id before saving option.

As workaround I remove the condition of foreign key for option and allow the inst_id to be null. It works now correctly, the inst_id of the Option won't be set!!!

Any idea?

Regards,
Reza


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 1:54 pm 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
I think you need to set constrained for the mapping

Try

<one-to-one
name="option"
class="Option"
fetch="join"
lazy="false"
property-ref="instrumentId"
cascade="all"
constrained="false"
/>


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

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.