-->
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.  [ 8 posts ] 
Author Message
 Post subject: Avoiding Automatic Dirty Checking problem
PostPosted: Mon Sep 03, 2007 11:39 am 
Newbie

Joined: Mon Jun 04, 2007 5:38 am
Posts: 6
Location: UK
Hi,

I'm using Hibernate to develop the persistence services for a thick, Swing-based application - the data source (a HSQLDB database) is only ever used by a single instance of the application. After reading around about using Hibernate with thick clients, I opted for a single, long-lived hibernate session in my DAO subsystem.

Here's the problem I face: objects returned from the persistence layer to the GUI can be changed by the user. However, I only want those user changes to be persisted when the user selects the 'OK' or 'Save' button (i.e. by an explicit call to Session.update()). By default, Hibernate performs automatic dirty checking, which means that those user changes are applied straight away! What's the best way of dealing with this situation? I've heard a little about read-only transactions - will they give me what I'm looking for?

Thanks,

Peeper.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 04, 2007 2:51 am 
Expert
Expert

Joined: Thu Sep 04, 2003 8:23 am
Posts: 368
You can use FlushMode.MANUAL

But long live session does not mean unique transaction. You may define transaction boundaries based on screen validation and rollback the transaction when the user cancel the screen

_________________
Seb
(Please don't forget to give credits if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 04, 2007 5:28 am 
Newbie

Joined: Mon Jun 04, 2007 5:38 am
Posts: 6
Location: UK
Thanks for the suggestion, scesbron, but I don't see how using FlushMode.MANUAL will solve the problem. As far as I can see, FlushMode.MANUAL will only delay the act of writing to the database.

Also, I'm unsure what you mean by your statement
Quote:
But long live session does not mean unique transaction. You may define transaction boundaries based on screen validation and rollback the transaction when the user cancel the screen
.
Do you really mean transactions (rather than sessions)? Would you explain further, please?

Thanks,

Peeper.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 04, 2007 3:20 pm 
Beginner
Beginner

Joined: Tue Oct 10, 2006 3:23 am
Posts: 33
You might want to use an interceptor and mark the object dirty/clean on demand. I did that for forcing objects to be dirty.

Ie findDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types)

is pretty explicit

* RATE if this helps *


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 04, 2007 4:23 pm 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
you can use a StatelessSession, so the session will forget about objects it has fetched, and explicit calls to update() is required.

if you are using older versions of hibernate, call evict() after fetching the entity to detach it from session, merge it again using update().

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 05, 2007 5:12 am 
Expert
Expert

Joined: Thu Sep 04, 2003 8:23 am
Posts: 368
You can have one session with multiple transaction by doing a begin/commit for each action. With FlushMode.MANUAL, you don't commit your modification until you call session.flush in a transaction.
This is explained in chapter 11 of last gavin's book

_________________
Seb
(Please don't forget to give credits if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 06, 2007 6:59 am 
Newbie

Joined: Mon Jun 04, 2007 5:38 am
Posts: 6
Location: UK
mooritze wrote:
You might want to use an interceptor and mark the object dirty/clean on demand. I did that for forcing objects to be dirty.

Ie findDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types)

is pretty explicit


From the API docs, findDirty() is called from flush(). However, I don't see how I can determine whether or not the entity has been explicitly updated (say, with a call to update()). There's an onSave() method on the Interceptor interface, but the documentation is lacking - I could grovel about in the source to get the contextual information I'm looking for.

I'd be grateful of any suggestions or clarification.

Thanks,

Peeper.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 06, 2007 7:04 am 
Newbie

Joined: Mon Jun 04, 2007 5:38 am
Posts: 6
Location: UK
pasha wrote:
you can use a StatelessSession, so the session will forget about objects it has fetched, and explicit calls to update() is required.

if you are using older versions of hibernate, call evict() after fetching the entity to detach it from session, merge it again using update().


A StatelessSession would provide detached objects to the client. That would cause problems when using lazy loading (which I am) in a thick client. That's because thick clients don't have the well defined small units of work within which lazy loading occurs (c.f. web applications).

Thanks,

Peeper.


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