-->
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: detecting changes in an entity.
PostPosted: Sun Dec 03, 2006 10:24 am 
Newbie

Joined: Wed Nov 29, 2006 2:49 am
Posts: 5
I am a newbie in Hibernate. I would like to detect if a certain detached entity has been changed (without loading the entity again and comparing field by field) and then make some decision. How can I do this?
Thankyou.

_________________
A newbie in Hibernate


Top
 Profile  
 
 Post subject: detecting changes in an entity
PostPosted: Sun Dec 03, 2006 6:46 pm 
Beginner
Beginner

Joined: Thu Apr 27, 2006 12:19 pm
Posts: 33
Location: Seattle, WA
I'm assuming your detached objects are no longer associated with a Hibernate Session, which of course would handle automatic dirty checking. What I've done before is set a boolean flag in each of my object's setters if the value has changed.

private boolean changed = false;
private String firstName;

public void setFirstName(String firstName)
{
setChanged(this.firstName != firstName);
this.firstName = firstName;
}

protected void setChanged(boolean changed)
{
if (changed)
this.changed = changed;
}

I'm not sure of another way to do this without hitting the database again.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 11, 2006 4:13 am 
Newbie

Joined: Wed Nov 29, 2006 2:49 am
Posts: 5
Thanks for the reply sneal.

Just pushing it a lil further, is there a built in method in hibernate or a pattern I can use to tell which field in the entity has changed in the detached entity? (In the worst case I would have to read from the database and compare field by field)
Thanks.
Mutahi.

_________________
A newbie in Hibernate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 11, 2006 7:18 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
mutahi wrote:
In the worst case I would have to read from the database and compare field by field



If you want access the database in the worst case you may use select-before-update attribute on the class mapping to tell Hibernate to automatically perform dirty checking and avoid unnecessary update query if detached obect didn't cange or use versioning mechanism support in Hibernate.

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 11, 2006 8:25 am 
Newbie

Joined: Wed Nov 29, 2006 2:49 am
Posts: 5
Thanks for the reply Lester,
While I like this solution, I still don't get to know which field in the detached entity has changed. Any Ideas?.
Thanks.
Mutahi.

_________________
A newbie in Hibernate


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.