sivaramom (3) [Avatar] Offline
#1
I am reading the book Hibernate in Action (latest Early edition). I came across "In order to avoid unnecessary updates in dirty checking in the case of string or any other object other than collections, new instances need to be created --page 30 of the early edition book

public String getitem(){
return new String(this.itemvalue); }

please explain in detail the need behind this behaviour

Stackoverflow Link:
http://stackoverflow.com/questions/21971570/hibernate-unnecceessary-updates-because-of-dirty-checking
Christian Bauer (56) [Avatar] Offline
#2
Re: hibernate unnecceessary updates because of dirty checking
Neither your quote or the code can be found in the book, and indeed, it doesn't make any sense. I think you should read it again.
sivaramom (3) [Avatar] Offline
#3
Re: hibernate unnecceessary updates because of dirty checking
page 30 of MEAP v7,
Another issue to consider is dirty checking. Hibernate automatically detects state changes in order to
synchronize the updated state with the database. It's usually safe to return a different instance from the
getter method than the instance passed by Hibernate to the setter. Hibernate compares them by value—not
by object identity—to determine whether the attribute's persistent state needs to be updated. For example,
the following getter method doesn't result in unnecessary SQL UPDATEs:
// This is OK
public String getFirstname() {
return new String(firstname);
}

Message was edited by:
sivaramom

Message was edited by:
sivaramom
Christian Bauer (56) [Avatar] Offline
#4
Re: hibernate unnecceessary updates because of dirty checking
Nowhere does it say "in order to avoid". It says that even if you do this, it's ok. Obviously you shouldn't do this...
sivaramom (3) [Avatar] Offline
#5
Re: hibernate unnecceessary updates because of dirty checking
Got it.Thanks very much for your time and reply.