-->
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: Maintaining an Object across several NH Sessions
PostPosted: Tue Sep 20, 2005 8:41 am 
Newbie

Joined: Mon Jun 06, 2005 10:33 am
Posts: 6
Hi,

In my ASP.Net app, I store the NH Session in the HttpContext = One NH Session per User Request for the page.

Here are the 3 steps to present my problem:

1. I have a page that initially Load()s an object from the NH Session and stores it in the ASP.Net Session.

2. In subsequent PostBacks I retrieve the object from the ASP.Net Session and work on it.

3. Finally, when the user clicks a "save" button, I would like to Save() the object to the DB using the NH Session.

The problem is that the NH Session in step 1 is already dead and gone by the time I get to step 3. In step 3 I get nasty exceptions like "Another object was associated with this id" etc.

Is there some way I can "un-evict" the object into the NH Session is step 3 even though it was never really evicted from that NH Session?

Shouldn't SaveOrUpdate() do this?


Thanks in advance,
urig

_________________
urig


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 14, 2005 5:11 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
Can't you do this on the 1st page:

Session.Evict(myObj);

//go through a couple of pages


Then associate myObj with the current session when you're ready to save? Tobin Harris did something like this on his blog, IIRC.

You may want to read this: http://www.tobinharris.com/nhibernateasp.aspx


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 15, 2005 7:04 am 
Beginner
Beginner

Joined: Mon Oct 03, 2005 4:59 am
Posts: 26
Location: Cambridge, UK
I do it almost exactly as described in the original post for my asp.net pages, and it works fine. E.g., on one page, something like this (typed in browser from memory, please treat as pseudocode)
Code:
protected void Button1_command( object sender, CommandEventArgs e )
{
    //retrieve a person and store them in the user session
    int id = int.Parse( e.CommandArgument.ToString() );
    ISession s = (ISession) Context.Items["NHSession"];
    Person p = s.Get( typeof( Person ), id );
    Session["CurrentPerson"] = p;
    s.Close();
}

And on some other page, or a different event of the same page,
Code:
protected void btnSavePerson_click( object sender, EventArgs e )
{
    ISession s = (ISession) Context.Items["NHSession"];
    Person p = (Person) Session["CurrentPerson"];
    s.SaveOrUpdate( p );

    p.Name = textName.Text;
    p.Age = int.Parse( textAge.Text );

    s.Flush();
    s.Close();
}


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.