-->
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.  [ 4 posts ] 
Author Message
 Post subject: Check if a property is dirty?
PostPosted: Tue Apr 22, 2008 12:05 pm 
Newbie

Joined: Sat Sep 03, 2005 1:10 pm
Posts: 5
I know this isn't an advertised feature of Hibernate, but I'd like to be able to check if a property has been changed. Other than doing it myself (by keeping the original value, or adding logic to the setter), is there any way to ask Hibernate if an entity's property has been changed? It seems like it should have the information, but it doesn't seem accessible.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 4:21 pm 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
I think you have to write an Interceptor like this:
Code:
import org.hibernate.EmptyInterceptor

public class HistoryInterceptor extends EmptyInterceptor {
(....)
public boolean onFlushDirty(final Object pEntity,
                              final Serializable pId,
                              final Object[] pCurrentState,
                              final Object[] pPreviousState,
                              final String[] pPropertyNames,
                              final Type[] pTypes)


then you have to check for the entity you are watching and compare previous state with current state and do log what you want.
But be careful, this interceptor is called while Hibernate is flushing the new state to the database.

Carlo

--------------
give me credits if this helped you.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 24, 2008 11:05 am 
Newbie

Joined: Sat Sep 03, 2005 1:10 pm
Posts: 5
That won't really help me. I need to know if the object is dirty while processing business logic (well before writing the object to the database).

Let me restate the problem: I have an Entity that contains a list. At a point in our business logic, I check the list. If the user has added one of 4 values in this session (ie, they didn't come from the database), I need to perform some logic.

Currently my solution is to drop down to JDBC and get the values manually, and then check them against the Entity. I had to use JDBC so I didn't end up with the cached data. Another solution would be to keep a copy of the list when I load it from the database, but that seemed messy too. I would like to simply ask hibernate what has changed in the list. It seems like it should already know.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 24, 2008 11:17 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
well, I think what you want from Hibernate is, at least to my knowledge, not available as an API.
Hibernate does dirty checking only if you want to persist your objects to the database. If you have detached objects at this moment, Hibernate reloads the objects from the database; otherwise it has a copy of them in it's entity cache. Then it compares these objects against your current objects to decide which need to be updated (or inserted) into the db.

So, back to your problem: I think you have to keep track of the object changes yourself. I suppose the best strategy is to hold a copy of the objects state as they were loaded from database. So, regarding this, you are doing in effect the same as Hibernate itself does (provided your session is not closed). And then it would of course be a great help if Hibernate could give as an API to do comparisons like these.

Carlo


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