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.  [ 7 posts ] 
Author Message
 Post subject: Can object hydration be intercepted somehow?
PostPosted: Thu Oct 06, 2005 11:34 pm 
Beginner
Beginner

Joined: Fri Jun 10, 2005 2:22 pm
Posts: 27
Hibernate version: 3.0.5

I would like to be able to "intercept" the hydration of objects that are loaded from the database so that I can do some extra things to them before they are made available to the application. Is there any kind of existing hook that would allow this, or could someone point me to the class or classes that load the persistent objects so that I can maybe figure it out myself. I'm most interested in the code that loads and sets the object Id.

Thanks,
David


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 7:28 am 
Newbie

Joined: Fri Oct 07, 2005 6:36 am
Posts: 7
You know that you can implement some stuff just in the setter for the object id? Hibernate will per default access properties via accessor methods so you could stuff some logic in there.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 7:46 am 
Newbie

Joined: Thu Oct 06, 2005 12:30 pm
Posts: 10
Take a look at the org.hibernate.Interceptor interface:

http://www.hibernate.org/hib_docs/v3/ap ... eptor.html

Once implemented, you can register this interceptor at the global-configuration or per-session level.

--
Josh


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 10:04 am 
Beginner
Beginner

Joined: Fri Jun 10, 2005 2:22 pm
Posts: 27
Yes, I am aware of the Interceptor interface, but it doesn't really satisfy my needs. Adding code to the setId() method doesn't really help, either.

I'm am attempting to use the Id in a little different manner for transient objects, but I need to know if the object is transient or persistent. In all cases they will be disconnected. So, what I'm looking for is a way to have Hibernate set a flag for me that lets me know that the object is persistent and not transient.

Thanks,
David


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 11:49 am 
Beginner
Beginner

Joined: Mon Sep 27, 2004 4:28 pm
Posts: 44
There's an interface in the .classic package that your entities may implement. It includes a method called onLoad().. that may help you.

Additionally, there are new event interfaces you can use. They're unfortunately not implemented as observer patterns (where you could just add yourself to the list)... but you can override the default implementation and hook yourself in. I believe the class to look at is:

org.hibernate.event.def.DefaultLoadEventListener

Phill


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 11:50 am 
Beginner
Beginner

Joined: Mon Sep 27, 2004 4:28 pm
Posts: 44
Sorry.. forgot to tell you what the interface in classic is called.. it's called Lifecycle.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 1:27 pm 
Beginner
Beginner

Joined: Fri Jun 10, 2005 2:22 pm
Posts: 27
Excellent! I thought I remembered there being an interface that would do what I wanted, and it looks like this will do the trick.

Thanks!


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