-->
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: Original Entity Values
PostPosted: Mon Jan 15, 2007 8:07 am 
Newbie

Joined: Tue Jan 09, 2007 6:25 pm
Posts: 3
Hi,

Is there a way to get the original values of a property using Hibernate?

i.e. For all our entities we need to support an addiational getter "getOriginalValue()", as Hibernate uses an internal cache to maintain the state or to find out "isDirty()", is it possible to expose this functionality to the application?

Thanks,
Mel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 15, 2007 12:01 pm 
Regular
Regular

Joined: Wed Dec 07, 2005 4:19 pm
Posts: 53
You may look into using the Interceptor interface.

You attach it to your session:
Code:
getSessionFactory().openSession(myInterceptor)

and then whenever Hibernate flushes the session, onFlushDirty(...) gets called, with a list of 'original' property values. The list of original values will be empty (null) if you happen to flush a (previously) detached object, or a new object instance.

Granted, it is not exactly what you are asking for, but maybe it could help.

Another option is [possibly a way to implement session.isDirty(Object obj)]
Code:
PersistenceContext ctx = session.getPersistenceContext();

using methods such as:
Code:
EntityEntry entry = ctx.getEntity(EntityKey key)
Object props = entry.getLoadedState(String propertyName)

but I admit I have not done it - and there is probably more to consider.

Don't forget to rate this reply (if it did help).
Martin


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 15, 2007 12:57 pm 
Regular
Regular

Joined: Wed Dec 07, 2005 4:19 pm
Posts: 53
I wrote:

mbrunecky wrote:
Another option is [possibly a way to implement session.isDirty(Object obj)]

This approach may be far more than where you want to go - starting with the fact that getPresistenceContext() is an implementation detail (SessionImpl), and the complexity goes up from there.
There are many possibilites (starting with a variety of states in which your object may be - from not initialized to deleted), and a lot of Hibernate internals...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 9:45 am 
Newbie

Joined: Tue Jan 09, 2007 6:25 pm
Posts: 3
Hi Martin,

The tip was useful, I already found the persistentContext.

For some reason the 'persistentContext' was not exposed via the session interface but via sessionImpl.

It works fine as long as the entity is attached to a session.

Thanks,
Mel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 3:54 pm 
Regular
Regular

Joined: Wed Dec 07, 2005 4:19 pm
Posts: 53
lud0h wrote:
The tip was useful, I already found the persistentContext.

Please, do not forget to rate my post then... Thanx

Quote:
It works fine as long as the entity is attached to a session.

Almost everything in Hibernate 'works' only within a session scope.
Detached objects only work when 're-attached' back - and then they are then considered totally dirty.

The original book warned against long sessions, but without them, why use Hibernate? The new book favors long sessions in 'one conversaction or one thread', but one has to be carefull.

We over-did it, and our very-long-sessions ended up in a hell of evictions (trying to avoid caching things forever).
So our latest invention are 'session objects' - object hierarchies carrying session along with them, closing session when we dispose of them. Also helps keeping our transactions separate (as Session transaction commits ALL session changes ... well, unless...).

Martin


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