-->
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.  [ 9 posts ] 
Author Message
 Post subject: Lifecycle: how to get it work?
PostPosted: Fri Jan 30, 2004 2:08 am 
Newbie

Joined: Fri Jan 30, 2004 1:59 am
Posts: 10
Hi,

I try to use Lifecycle. I want to use it to change a some data just before it is persisted. But, the Lifecycle methods are never called!

I have persitent object. something like this:

Code:
public class Base implements Lifecycle, Serializable {
//bla bla
   public boolean onUpdate(Session s)  throws CallbackException  {
      log.info("start");
      //bla bla
   }
}


And I call update:

Code:
//bla
s.load(ltg, new Long(430497));
ltg.setSourceUrl("Something new.nl");
s.update(ltg);
//bla


The object is persisted nicely, but onUpdate() is never called. What am i doing wrong?

Jaap


Top
 Profile  
 
 Post subject: Re: Lifecycle: how to get it work?
PostPosted: Fri Jan 30, 2004 1:24 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Jaap wrote:
The object is persisted nicely, but onUpdate() is never called. What am i doing wrong?

You did not read the API and reference doc :)
Have a look at interceptor for that.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Manual?
PostPosted: Mon Feb 02, 2004 6:53 am 
Newbie

Joined: Fri Jan 30, 2004 1:59 am
Posts: 10
I actually read both - and found interceptor not really suitable for my needs.

I would say that:
"Optionally, a persistent class might implement the interface Lifecycle which provides some callbacks that allow the persistent object to perform necessary initialization/cleanup after save or load and before deletion or update."

means my persistent class will get the opportunity to do some things before an update is actually performed by Hibernate. But the methods are not called. Interceptor is different and not suitable because it needs to be invoked explicitly.

But problably I do something wrong or I have misread the manual.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2004 6:57 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
From the API.
Called when an entity is passed to Session.update(). *This method is not called every time the object's state is persisted during a flush*.

An object already associated with the session (ie loaded), does not need to be update(), this method will be silently ignored. In your cas, it's the case and onUpdate won't be called.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 03, 2004 7:37 am 
Newbie

Joined: Fri Jan 30, 2004 1:59 am
Posts: 10
Hmmmm,

Still find it confusing - but found the solution in the manual anyway. It is so simple - I think like I have looked like a cow staring to it's fence...

Anyway - the reason why I wanted 'Lifecycle' in the first place is because of what i see as a bug in Hibernate. Every time an object persists - I want an 'updateDate' field be set to the system time. Therefor I have mapped a <timestamp> like this:

Code:
<timestamp column="update_date" name="updateDate" unsaved-value="null"/>

But when updating an object for the first time - hibernate throws an error. The actual query generated by hibernate is something like:
Code:
Hibernate: update jms_external_listings set update_date=?, address=?, bla bla where id=? and update_date=?


I don't know the details about JDBC (MySQL) but in plain SQL this is wrong. Updating an object for the first time means the the timestamp is NULL. So the query in that case should not be "= null" but "is null".


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 03, 2004 8:20 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
IMHO it should not be null, it should be null before the insert, which should set it for the first time.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 03, 2004 9:07 am 
Newbie

Joined: Fri Jan 30, 2004 1:59 am
Posts: 10
For the concurrency functionality this would not matter if it is null of not - I think 'bug' are not the right words i choose. I have a legacy which cannot be implemented by Hibernate with the timestamp (just almost except this damn null value :-)

For database design - it is IMHO better to avoid to fill records with unnecessary info*. So if a field contains no information - keep it null! Most of the records in my case are only created - and never updated - so why waste this space?
The design 'rule of thumb' can be especially important when you use indexes (one large table has an index on update_date in my case).

*all my tables have standard creation_date and update_date columns - update_date should - as it name says - only be filled on update. But i see it is actually a specific implementation of timestamp by me - but still if you you tmestamp _only_ for versioning - avoid unnecessary information in the database.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 03, 2004 9:14 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Well you notice that timestamp is for optimistic locking, which makes it absolutely the wrong choice in your setup. I would suggest mapping this as a plain old property, and handling the setting yourself either in your code or in an interceptor. I would still prefer using triggers for such stuff, but I have a strange feeling you are on mysql ...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 03, 2004 1:51 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I would think that it is most natural to set the updatedTime to the current time when the row is INSERTed. I'm happy with current functionality.


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