-->
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.  [ 8 posts ] 
Author Message
 Post subject: one-to-one save an already saved object
PostPosted: Tue Dec 23, 2003 11:47 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I'm having some trouble whith Hibernate 2.0.3

Here is the case.
I have a mono-directional one-to-one relationship with a foreign generator.
I save the non mapped side object in one session, close it
Then I save the mapped side object and hibernate recreate a new non-mapped side object. I can't understand why.
Here are some materials

Code:
session = sf.openSession();
Transaction tx = session.beginTransaction();
Dumb dumb = new Dumb();
session.save(dumb);
tx.commit();
session.close();
Long oldDumbId = dumb.getId(); //copy id
       
//new session       
session = sf.openSession();
tx = session.beginTransaction();
Dumber dumber = new Dumber();
dumber.setDumb(dumb);
session.save(dumber);
tx.commit();
session.close();
assertEquals(oldDumbId, dumb.getId()); //this fail, actually a new dumb is inserted in DB with a new id


the previous code with session.saveOrUpdate(dumb); before session.save(dumber); works.

Here is the mapping
Code:
<hibernate-mapping>
  <class name="sample.Dumb" table="dumb">

    <id name="id" column="id" type="long" unsaved-value="null">
      <generator class="sequence">
        <param name="sequence">SEQ_DEFAULT</param>
      </generator>
    </id>
  </class>
</hibernate-mapping>

<hibernate-mapping>
  <class name="sample.Dumber" table="dumber">

    <id name="id" column="id" type="long" unsaved-value="null">
      <generator class="foreign">
        <param name="property">dumb</param>
      </generator>
    </id>
   <one-to-one class="sample.Dumb" constrained="true" name="dumb"/>
  </class>
</hibernate-mapping>


Is this the expected behavior ? Why hibernate does not execute a saveOrUpdate() on dumb instead of save() when saving dumber ?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 31, 2003 6:45 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Submit to JIRA.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 31, 2003 11:44 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I dug the subject and found that it was solved in the 2.1 familly (using getEntityIdentifierIfNotUnsaved() in the foreign generator instead of brutal save()).
This is a minor and very rare issue, I don't know if it's useful to backport the 2.1 fix.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2004 4:19 pm 
Newbie

Joined: Mon Jan 12, 2004 3:25 pm
Posts: 9
I have the same situation except my direction needs to go the other way.
So I would have a mapping like:

Code:
<hibernate-mapping>
  <class name="sample.Dumb" table="dumb">

    <id name="id" column="id" type="long" unsaved-value="null">
      <generator class="sequence">
        <param name="sequence">SEQ_DEFAULT</param>
      </generator>
    </id>

    <one-to-one class="sample.Dumber" constrained="false"  name="dumber" cascade="all" />

  </class>
</hibernate-mapping>

<hibernate-mapping>
  <class name="sample.Dumber" table="dumber">

    <id name="id" column="id" type="long" unsaved-value="null">
      <generator class="foreign">
        <param name="property">dumb</param>
      </generator>
    </id>
  </class>
</hibernate-mapping>


the foreign generator does not make sense anymore, but I was wondering how would I go about setting this relation?
How would I tell hibernate to fill the id of the Dumber?

Does anyone have a clue?

Thanks,
Alex.[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2004 4:46 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Use foreign generator in Dumb, and sequence in Dumber I'd say


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2004 5:01 pm 
Newbie

Joined: Mon Jan 12, 2004 3:25 pm
Posts: 9
Makes sense, but my assocation is 1 (Dumb) to 0..1 (Dumber) and solution you suggest will break in case there are 0 Dumber for a Dumb.

Any other suggestions?

Thanks,
Alex.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2004 9:20 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Set a bi-dir

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 10:03 am 
Newbie

Joined: Mon Jan 12, 2004 3:25 pm
Posts: 9
that is what I did for now,
but I really only need one direction

it is quite strange, a simple thing like
this is hard to describe. I guess I can
complain to the "authorities" :-)

Thanks,
Alex.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.