-->
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: Avoid UPDATEs when cascading?
PostPosted: Sun Oct 02, 2005 10:59 am 
Newbie

Joined: Mon May 16, 2005 1:25 pm
Posts: 9
Hello,

When class contains a one-to-many relation with a cascade attribute, objects are inserted in three stages. First insert is performed for the parent object, after that all child objects and at the end all child objects updated with their parent id.

For example (see mapping below) when storing A that has a reference to B instances. First A is inserted, then all Bs are inserted and then all Bs are updated with A's id.

The question is how (if possible at all) to exclude updates executed on Bs?

Thanks,

Alex

--- Info:

Hibernate version: 3.0.5

Mapping documents:

<hibernate-mapping>

<!-- class A -->

<class table="A_TAB" name="myPackage.A">
<id type="java.lang.String" column="ID" length="32" unsaved-value="null" name="id">
<generator class="uuid.hex"/>
</id>
<set fetch="join" lazy="false" cascade="all-delete-orphan" name="attributes">
<key column="B_ID"/>
<one-to-many class="myPackage.B"/>
</set>
</class>

<!-- class B-->

<class table="B_TAB" name="myPackage.B">
<id type="java.lang.String" column="ID" length="32" unsaved-value="null" name="id">
<generator class="uuid.hex"/>
</id>
<property name="name" index="NAME" not-null="true" type="string" column="NAME"/>
<property name="value" type="string" column="VALUE"/>
</class>
</hibernate-mapping>


Name and version of the database you are using: MsSql 2000


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 11:05 pm 
Newbie

Joined: Thu Sep 08, 2005 9:27 am
Posts: 10
Hi Alex,
When you say
Quote:
exclude updates executed on Bs

you mean that when A is updated, you want to avoid the B childs to be updated too. Am I right?

In that case, why would you want to do that?

Have you tried to implement the Interceptor interface? It provides callbacks so that you can explicitly control saves, updates and deletions.

Good luck!

_________________
Pablo N. Alvarez
Austral University
Argentina


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 09, 2005 6:20 am 
Newbie

Joined: Mon May 16, 2005 1:25 pm
Posts: 9
Hi pablius,

Thanks for the reply.

You are right, I want that everything will be updated.

But the question was why there is a need in a separate UPDATE statement. The B's row can include the A's ID that can be set by a single INSERT .

Thanks,
Alex


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 09, 2005 9:32 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
alexg wrote:
Hi pablius,

Thanks for the reply.

You are right, I want that everything will be updated.

But the question was why there is a need in a separate UPDATE statement. The B's row can include the A's ID that can be set by a single INSERT .

Thanks,
Alex


It's because your object B doesn't contain a reference back to A.

If B had a <many-to-one> relationship pointing back to the parent, the ID of A would be assigned to B when it was generated and stored in the database. Without the relationship, there is no place to put the ID on the B object.

I'm a little confused by your mapping to be honest. Assuming B_ID is the PK of B, how is it the KEY to the B collection on A ?

Read Section 22.2 of the 3.0.5 docs for more information.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 09, 2005 12:44 pm 
Newbie

Joined: Mon May 16, 2005 1:25 pm
Posts: 9
Thanks for the answer!

Sounds like I have to have use inverse attribute when declaring set of Bs:

...
<set fetch="join" lazy="false" cascade="all-delete-orphan" name="bs" inverse="true" >
<key column= "A_ID" />
<one-to-many class="myPackage.B"/>
</set>

Quote:
It's because your object B doesn't contain a reference back to A.


I think with the inverse attribute there is no need in a back reference...

Quote:
I'm a little confused by your mapping to be honest. Assuming B_ID is the PK of B, how is it the KEY to the B collection on A ?


You are right, the mapping was incorrect... It should include A_ID as a key.



Thanks for your help,

Alex


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.