-->
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: Trying to prevent Nhibernate from writing to DB until Flush
PostPosted: Tue Jun 28, 2005 4:55 pm 
Hi,
I am trying to do the following, is it possible?

I want a form where the user can create a new parent and attach some new children. So when the user creates the new parent object, I dont want to save it to the database, but I do want it to be saved to the session.

so I want to call
session.save(parent)
then add some children to the
parent.children IList.
then if the user clicks Apply or OK I want to call a
session.flush().

However.. (even if I set the flush mode to NEVER) Nhibernate is flushing to the database after I call Save??? So here is what im seeing

FORM OPENS
CONSTRUCTOR:
CREATE A NEW PARENT TYPE OBJECT P
SET P TO SOME PLACE HOLDER DATA
session.save(P)

OKBUTTON:
SET P TO CORRECT DATA
session.flush()

I have also tried that with a transaction, where I would begin the transaction before session.save, and commit it instead of flush.
It did the same thing.

Does anyone have a working example of what I have described?
A simple Create form that does not persist to the DB until the user clicks OK or Apply? Or is this not possible with Nhibernate?


Top
  
 
 Post subject:
PostPosted: Wed Jun 29, 2005 2:45 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
I don't think what you want is possible with ISession.Save(). It may be possible to simply Lock() a transient object to an ISession, so that it will be saved upon the next Flush(). You might give that a try.

If it doesn't work, you should be able to simply execute the Save() after the children have been added. With appropriate cascading and inversion, the whole graph will be persisted in one shot.

Also, you might consider leaving the behavior you have as is. It gives your user a clear "save-point"; if his work is interrupted, at least the parent isn't lost.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 30, 2005 12:24 pm 
Thanks for the repsonse,

I took a glance at the Wilson Nhibernate example.. I couldnt get it to actually run for some reason (exception in the Nhibernate.dll, didnt have time to debug)

What I saw was that he was instantiated his IList's in the Nhibernate object files.

IList something = new ArrayList();

This would allow a user to Add Children to an unsaved parent. And when they click OK on the parent it would loop through the children Ilist and save.

It didnt work though. I assume because the IList wasnt created by Nhibernate it threw up. (Gave a could not save or update row not found error)

So now i've hacked it (really not a great hack) by adding an arraylist to the object. So as an example, I have a Car object that has Tire's as children

IList Tires
ArrayList NTires

So when creating a new Car, any tires added would go into the NTires, then when you click OK on your car, it would save the Car, then loop the NTires and call Save on them, setting their parent to be Car.

What a horrible solution. Has anyone been able to create the IList Tires manually and have it persist properly?


Top
  
 
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.