-->
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.  [ 10 posts ] 
Author Message
 Post subject: saveOrUpdate not calling Lifecycle.onUpdate or Lifecycle.onS
PostPosted: Thu Nov 13, 2003 4:52 pm 
Beginner
Beginner

Joined: Wed Sep 17, 2003 10:25 am
Posts: 36
When using saveOrUpdate oin one of my entity classes that implement Lifecycle, Lifecycle.onUpdate or Lifecycle.onSave are not called at all. Is this how it is suppose to work or is this a bug? I am using Hibernate 2.0.3.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2003 7:59 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
read the javadoc


Top
 Profile  
 
 Post subject: read javadoc
PostPosted: Fri Nov 14, 2003 12:34 pm 
Beginner
Beginner

Joined: Wed Sep 17, 2003 10:25 am
Posts: 36
Gavin,

Thanks for responding. I actually have read the Javadoc and was expecting the when session.saveOrUpdate is called that either save or update will be called on the passed entity. But this is not what I am seeing in my application. This is working when I just use save or update though.

Thanks,

Haytham


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 14, 2003 12:37 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
And I quote:

Code:
* <b>onUpdate:</b> called just before an object is updated,
* ie. when <tt>Session.update()</tt> is called<br>

Code:
    * Called when an entity is passed to <tt>Session.update()</tt>.
    * This method is <em>not</em> called every time the object's
    * state is persisted during a flush.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 14, 2003 12:44 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Oh, perhaps I have misunderstood your misunderstanding.

You probably think that you can call saveOrUpdate() on an entity that is already associated with the session.

Well, you can, but it is a no-op. Hibernate does automatic dirty checking.

onUpdate() is called when a detached instance is passed to update().


Top
 Profile  
 
 Post subject: Ohh..that explains it..
PostPosted: Fri Nov 14, 2003 1:28 pm 
Beginner
Beginner

Joined: Wed Sep 17, 2003 10:25 am
Posts: 36
I think I understood what is going on. Here is what I am doing:

I am loading an entity (lets call it EntityA) and adding to one of its collections (called myBs) another entity type (EntityB) then calling saveOrUpdate on it EntityA. This is not resulting in a call to onSave or onUpdate on the parent entity. If I update an entity's collection, will this kind of update be detected by Hibernate as dirty?

My configuration is as follows:

<set name="myBs" lazy="true" table="table_linking_a_to_b">
<key column="a_id"/> <!-- id of entity_A -->
<many-to-many class="my.package.EntityB" column="b_id"/>
</set>

Both entities (A & B) are loaded first then associated by adding an EntityB to EntityA's myBs collection.

Here is a snippet of my code:

//load a
EntityA a = (EntityA )session.load(EntityA .class, a_Id);

//load b, could do this multiple times
EntityB b = (EntityB)session.load(EntityB.class, b_id);

a .getMyBs().add(b);

//I called either one of the following and neither resulted in
//a call to onSave or onUpdate
session.saveOrUpdate(a);
session.update(a);


session.flush();

What could be changed to make this work? It seems just updating EntityA's collection is not enough to trigger an onUpdate call on EntityA.

Thanks.


Top
 Profile  
 
 Post subject: Re: Ohh..that explains it..
PostPosted: Fri Nov 14, 2003 1:47 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
abulheath wrote:
//I called either one of the following and neither resulted in
//a call to onSave or onUpdate
session.saveOrUpdate(a);
session.update(a);

as Gavin said, Since you loaded A and B from the session, they are already associated to the session. You can remove sessionsaveOrUpdate(a) or session.update(a) : it's useless. Hibernate knows about dirtyness of a (and b) and won't call LifeCycle onUpdate.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: I guess you missed my question
PostPosted: Fri Nov 14, 2003 2:03 pm 
Beginner
Beginner

Joined: Wed Sep 17, 2003 10:25 am
Posts: 36
epbernard,

I am updating EntityA by the following:

a .getMyBs().add(b);

My question was, does this make EntityA dirty? Seems like it does not.

I also posed the question for an alternative to get what I am trying to do.

If anyone dealth with ths same issue and has a solution to share, I would appreciate a second point of view.


Top
 Profile  
 
 Post subject: Re: I guess you missed my question
PostPosted: Fri Nov 14, 2003 2:27 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
abulheath wrote:
My question was, does this make EntityA dirty?
Quote:
Yes since a is responsible of the association (no inverse="true"). Having a dirty entity does not means onUpdate or onSave is executed. In your case it won't.

abulheath wrote:
I also posed the question for an alternative to get what I am trying to do.

Not an expert on that but an interceptor might help (with the onDirty stuffs).

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Interceptor
PostPosted: Fri Nov 14, 2003 4:14 pm 
Beginner
Beginner

Joined: Wed Sep 17, 2003 10:25 am
Posts: 36
Thanks. I have started looking at interceptor and the onFlushDirty method.

back to Hibernating...


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