-->
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.  [ 7 posts ] 
Author Message
 Post subject: Best Practice? Cancel editing new object
PostPosted: Fri Feb 06, 2004 12:52 pm 
Newbie

Joined: Sat Sep 13, 2003 5:48 am
Posts: 6
Location: Hannover, Germany
Hi everyone,

I'm working on a swing app with several edit dialogs. Each edit dialog handles a single domain object and has an "Ok" action for committing changes and a "Cancel" action for rolling them back.

When I edit a stored object (which already has an Id) cancelling is easy: I simply reload the object from the db to overwrite all changes made.

But how do I redo the changes made to a new object which doesn't have an Id with which I could reload it?

Do I have to implement clone() to create a backup-copy or can I use a service hibernate provides?

Any hints are welcome, thanks in advance, Stefan


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 1:18 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I don't really get your problem, if you have a new object and "cancel" is clicked, the new object should just be discarded or what do you want to do?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 5:34 pm 
Newbie

Joined: Sat Sep 13, 2003 5:48 am
Posts: 6
Location: Hannover, Germany
gloeglm wrote:
I don't really get your problem, if you have a new object and "cancel" is clicked, the new object should just be discarded or what do you want to do?


You're perfectly right. Sorry, it was my fault.
I should have mentioned that I'm dealing with a hierarchy of objects.

Let me describe an example scenario:

I have got Parent objects which can own Child objects. For each class, Parent and Child, I have a special editing dialog. Only Parent objects are stored explicitly since they contain their Child objects in a collection which is stored implicitly.
  1. I create a new Parent and start to edit it in the appropriate dialog.
  2. I add a new Child object to this Parent and start editing the Child in a sub dialog of the Parent's dialog (There's an "Add Child" action in the Parent's dialog).
  3. I commit the changes on the Child object by clicking OK in the subdialog. This OK action doesn't save the Child object because it will be saved together with its Parent. So the Child still has no Id.
  4. I start again editing the same Child object as in step 3 maybe because I think I made a mistake in steps 2+3. There also is an "Edit Child" action in the Parent's dialog.
  5. But now I cancel the sub dialog. So the Child object should be in the same state as just after step 3.

How can I resume the correct Child object state after step 5?
How do you deal with this kind of problem?
Is there any pattern or best practice?
Or do I really have to preserve the Child's state in a cloned backup copy? (I don't want to write a clone()-method because my real object hiearchy is not that simple as in the scenario above)

Thanks for your hints and opinions,
Stefan


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 5:42 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Well as far as Hibernate goes it can do nothing for it, because actually your child object is not persisted yet, so Hibernate does know nothing about it. Either you have to store the old state yourself somewhere (take a look at something like the Command Pattern, I think this could enable you to narrow down the ammount of state information you store). The other possiblitiy is to store more eagerly, eg. save the child every time ok is clicked.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 5:52 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Actually as long as you just want "cancel" functionality, this should be even easier ... just keep the data in the text field until the user clicks "ok" and only then push it to your data object.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 07, 2004 12:05 pm 
Newbie

Joined: Sat Sep 13, 2003 5:48 am
Posts: 6
Location: Hannover, Germany
Quote:
just keep the data in the text field

This would work fine if there were just textfields. But my object hierarchies are more complex. That's why many dialogs contain lists or tables with collections of dependent objects.

Slowly I come to the conclusion that cloning my object hierarchies would be the best solution to preserve backup-copies for the case of cancelling.
As I told you yesterday I don't like to implement all the clone()-methods for my domain objects. Today I tried the cloning-by-serializing approach which seems to work fine. I just copied the code from the following website:
http://www.javaworld.com/javaworld/javatips/jw-javatip76.html

So at last my question is answered. Dealing with Ok and Cancel has nothing to do with Hibernate. Thanks a lot for helping me think.

BTW: For testing my clone-mechanism I implemented an EqualsHelper class. After that I found a Hibernate2.1.1 class with the same name implemented by Gavin in last october which is rather empty. Is anyone interested in my more complete EqualsHelper?

Bye, Stefan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 06, 2006 7:47 pm 
Newbie

Joined: Tue Jun 06, 2006 7:33 pm
Posts: 11
Hi, I'm in the same situation as Stefan. I use clone to make IHM backup, I use Java deep clone.

the clone works great, bags (ArrayList) are deep copy.
If I remove a element from the bag, the update SQL, (delete) is executed from the original object
If I use the clone nothing else?? the SQL delete order is not executed...

Strange situation!
Any idea

Thank a lot


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