-->
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.  [ 3 posts ] 
Author Message
 Post subject: Newbie : Hibernate updates my entity on flusj
PostPosted: Fri Aug 11, 2006 9:07 am 
Newbie

Joined: Tue Jul 04, 2006 9:40 am
Posts: 4
Hibernate version: 3.1.2

I havent had much experience with Hibernate but we are working on making a Tapestry project with Hibernate back-end. I am facing a basic problem when an entity retrieved from Hibernate, if dirtied, is updated without an explicit "save" being issued. I reproduced the behaviour in a small code :

Code between sessionFactory.openSession() and session.close():

Code:
Client cl = new Client("John", "Smith", "john@yahoo.com");
      
Session session = factory.openSession();
session.save(cl);
session.flush();
session.close();

session = factory.openSession();

Client cl2 = (Client) session.get(Client.class, cl.getId());
cl2.setFirstName("James");

Address adr = new Address();
adr.setLine1("Someplace");
session.saveOrUpdate(adr);
session.flush();
session.close();


The log :

Code:
Hibernate: select max(id) from Client
Hibernate: insert into Client (first_name, last_name, email, id) values (?, ?, ?, ?)
Hibernate: select client0_.id as id0_0_, client0_.first_name as first2_0_0_, client0_.last_name as last3_0_0_, client0_.email as email0_0_ from Client client0_ where client0_.id=?
Hibernate: select max(id) from Address
Hibernate: insert into Address (line1, id) values (?, ?)
Hibernate: update Client set first_name=?, last_name=?, email=? where id=?


Without having issued an update on the Client entity, it is getting updated as part of the flush. This may work very badly with Tapestry because values in the POJOs get modified even during validation errors. Basically, dirty entities are bound to be around.

Is there anyway to avoid the entities being updated like this implicitly ? I know that the evict() method would solve but that means that I have to evict evey object being loaded from the DB !!

Pl help.

Thanks.[/code]


Top
 Profile  
 
 Post subject: No solution ?
PostPosted: Mon Aug 14, 2006 10:19 am 
Newbie

Joined: Tue Jul 04, 2006 9:40 am
Posts: 4
Is there no solution to this problem ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 14, 2006 12:06 pm 
Newbie

Joined: Fri Jun 09, 2006 8:03 am
Posts: 12
One way to "solve" this is to implement an Interceptor that overrides the findDirty method and returns an empty array. This tells hibernate not to notice any dirty changes. The problem with this is you can't update a database entry, only save.

I say "solve" because Hibenate's dirty checking is a feature not a problem. Persist objects are not Data Transfer Objects, they are the data. When you set a new value it is expected to get persisted right away.

The best bet if you still want to avoid dirty is to implement a deep copy method and make a new Instance after you call it back.


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