-->
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.  [ 2 posts ] 
Author Message
 Post subject: Save() is working but SaveOrUpdate() isn't
PostPosted: Fri Feb 17, 2006 8:21 pm 
Newbie

Joined: Tue Jun 07, 2005 4:39 pm
Posts: 10
I have an ASP.NET web application using NHibernate 1.0.1.0 running on IIS with MS SQL Server 2000 on the back end.

The application basically consists of a wizard-style interface that allows users to enter information over several pages. This information gets stored in one main object (BuildingPlan) that has a few one-to-one relationships and one-to-many relationships.

Since the UI spans several pages, I don't retrieve and update in the same Hibernate session. On the first page, the BuildingPlan is retrieved and put in the http session just as the user is forwarded to the next page:

BuildingPlan plan = mgr.RetrieveBuildingPlan("someplanid000");
Session["CurrentPlan"] = plan;
Response.Redirect("plan_submit_01.aspx");


After each page (I've only completed the first 2 so far), the information gets updated (or it should be anyway :) ). Now, entering info for a new plan works fine, and retrieving that info works. The problem is, when I bring up a previously entered plan, make changes, and then try to submit changes, the info in the database does not get updated. There are no errors and things look like they're working, but the changes are not persisted.

The following is the method the next page calls to submit changes to the various objects that are passed in with an IList (an ArrayList in this case).

private void StoreIList(IList list)
{
ISession session = factory.OpenSession();
ITransaction transaction = session.BeginTransaction();

for (int i = 0; i < list.Count; i++)
{
session.SaveOrUpdate(list[i]);
}

transaction.Commit();

session.Flush();
session.Close();
}


The other thing that is interesting is that if I create an object, store it, retrieve it in a seperate session, make changes, and then update it all in the code-behind then it DOES work. So my guess is that something about using the BuildingPlan object with ASP.NET server controls and/or storing the plan in session is interfering with something.

Any guesses or feedback? I'm sure it's probably something trivial I'm missing. I'm relatively new to ASP.NET.


Top
 Profile  
 
 Post subject: Solution
PostPosted: Wed Feb 22, 2006 7:16 pm 
Newbie

Joined: Tue Jun 07, 2005 4:39 pm
Posts: 10
After some extensive debugging, I figured out what was wrong and thought I'd post it here just in case anyone else is as big of a newbie as me and is stuck in the same situation. :)

The problem wasn't with NHibernate at all. It was caused by the Page_Load method. My Page_Load was set to retrieve whatever BuildingPlan object was in session. If one was found, it would then set the various textboxes, dropdownlists, etc... to the object values.

A seperate function would then be called when a submit button was clicked to update the properties with any changed values.

The problem was, after clicking the submit button, the Page_Load is called before the method that is fired when the button is clicked. I didn't know this until now. :)

So, I would update the value in the textbox on the page, click the submit button, and then the Page_Load event would take the information already stored in session, insert the old value into the textbox I had just altered, and THEN go on to call the method to update the BuildingPlan object. Since the textboxes and such were all reset because of the object in memory, the original information would be sent to NHibernate to store in the database (which it would just ignore since nothing had changed). That also explains why NHibernate didn't throw any errors since nothing was wrong as far as it was concerned.

So, I just put a if(!Page.IsPostBack) condition around the part of the code in Page_Load that fills in the values of the server controls and everything works beautifully now.


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