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: How to cascade save with predefined IDs?
PostPosted: Wed Mar 15, 2006 5:44 am 
Newbie

Joined: Wed Mar 15, 2006 5:33 am
Posts: 4
Hi,

I have understood that calling the save method could be performed in 2 different ways. Simpling calling the save method and letting hibernate generate the persistant object id, or giving to the save method the id we desire for the persistant object.

My question is simple :
Imagine that we want to save a whole group of objects, using cascades, and using pre-generated ID. As we cannot use the save method that requires an ID param in cascades, how can we do that?

I have tried calling the basic save method (the one requiring no id) but with an object whose ID is already set. On a single object it seems to work, but on a group of objects linked by associations (i.e. FK) and saved by cascade, it constantly raise errors like "row was updated or deleted by another transaction" or some exceptions telling that not-null FK are set to null.

To explain a bit, I'm willing to save a group of linked objects whose id are fixed by another application. So we need to save them with the provided ID.

Does somebody know how to do?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 15, 2006 10:42 am 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Hi,

Maybe providing a short example will make people start responding. Or not :).

Let's see the example, maybe somebody has an idea.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 15, 2006 11:10 am 
Newbie

Joined: Wed Mar 15, 2006 5:33 am
Posts: 4
You could for example wish to save a Foo object, having a mandatory Bar... For example with a "cascade all" param on the Foo-Bar association. The foo and all the bar will have ID already generated.

For example I have a myFoo object (that I want to save with a "abc" ID) which has 1 bar having a "vbn" ID. I want to save that group of object using cascades and not by saving each one of them separatly. BUT I want all of them to be saved with those predefined ID.
If I saved them one by one I could use for example save(myFoo, "abc"), but as I do want to use cascades I cannot.


How can I do? If I use basic save methods on objects whose ID attributes are already set (i.e. myFoo.setId("abc");save(myFoo); where the id java property is mapped as the ID (the PK) of the object), it generates problems such as the fact that the FK of the Foo table referencing the Bar table has a null value instead of having the "vbn" value... I use an IdentifierGenerator to generate ID. If the id property of the object is null, I generate a new ID in the generate method (that I return). If the id property is not null, I return its value because that's the value I want as the object PK. I have even tried to set the id attribute to null in the java object before returning the value, but the same exception occured... I find it weird that it does not work properly because the generator is called as if the fact that we save an object with a non null ID did not disturb hibernate.

I m using hibernate 3.1.2.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 15, 2006 12:29 pm 
Newbie

Joined: Fri Feb 17, 2006 10:31 am
Posts: 6
I have a similar situation too...

Say there are 3 tables where

A (prim key is APK),
B (child table to A; composite Id = (APK, N) - N is auto generated oracle sequence) and
C (child table to A; prim key = APK; foreign key to A constraint specified)

My mapping file is like

Table : A - id has sequenece generator (using oracle sequence)
Set with one to many mapping defined for B
Set with one to many mapping defined for C

Now when i save A, the primary key APK is being generated from sequence. But how do I specify the same generated sequence value to B and C and save them.

My save currently fails with "Exceptionorg.hibernate.NonUniqueObjectException". From what i debugged and understood, it is trying to load a previously persisted object instance.

Thanks,
Uma.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 15, 2006 12:36 pm 
Newbie

Joined: Fri Feb 17, 2006 10:31 am
Posts: 6
Sorry about the repeated posts. I was clicking the browser back button and it kept submitting.

BTW how do i delete a post? I could find the delete link/button in Edit POst.

Thanks,
Uma.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 16, 2006 5:03 am 
Newbie

Joined: Wed Mar 15, 2006 5:33 am
Posts: 4
Glad to see I'm not the only one in trouble in here. Sorry but I dunno how to delete a post...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 16, 2006 9:56 am 
Newbie

Joined: Fri Feb 17, 2006 10:31 am
Posts: 6
Hi,

Figured it for the Table A & Table C relationship. But i removed the one-to-many from A->C and have a bidirectional one-to-one.

Table A mapping :

Code:
   
     <  class name="ParentClass" ...>
         ...
       <id name="APK" type="long">
            <column name="TABLE_A_KEY_COL" precision="12" scale="0" />
            <generator class="sequence">
               <param name="sequence">TABLE_A_PK_SEQ</param>
            </generator>
        </id>
        ...
       <one-to-one name="ChildClass"
                class="ChildClass"
   cascade="all" /> 
       ...    
    </class>


Table C mapping:
Code:
     <class name="ChildClass" ...
      ...
       <id name="CFK" type="long" unsaved-value="null">
            <column name="TABLE_C_KEY_COL" precision="12" scale="0" />
            <generator class="foreign">
               <param name="property">ParentClass</param>
            </generator>
        </id>
          ...
     <one-to-one
          name="ParentClass"
          class="ParentClass"
           cascade="all"
           constrained="true"
           />
   ....
    </class>


This mapping automatically generates the APK using a oracle sequence and that sequence number is used for Table C

Now should try how it works for the composite id of Table B.

Thanks,
Uma.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 23, 2006 10:14 am 
Newbie

Joined: Wed Mar 15, 2006 5:33 am
Posts: 4
In fact I may have solved my problem. I'm not sure by now that it really works but I will write it down anyway, to try to help people that may have the same problem, as it seems no official hibernate member will help us :/ ...

I have changed the unsaved-value attribute to "undefined" (in the id section of your mapping files), and it seems to work. I did it after noticing that in the hibernate documentation, this configuration is mandatory for using an ASSIGN ID strategy. I use a generator strategy but is looks a little bit like the ASSIGN ID strategy (as I set some ID before saving objects).
As the documentation does not explain what the different values of unsaved-value do (I do not understand the difference between any and undefined for example) or how it really works, I am not sure it is the good thing to do, but by now it seems to work for ma.


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.