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.  [ 4 posts ] 
Author Message
 Post subject: Problem with Save followed by Load and proxies in IList(1.2)
PostPosted: Wed Apr 04, 2007 10:03 am 
Newbie

Joined: Wed Apr 04, 2007 9:40 am
Posts: 12
Hibernate version: 1.2 RC1

Mapping documents: Mapping works fine and may be too big for this post. If it becomes relevant, I will post it.

Code between sessionFactory.openSession() and session.close():
The openSession/closeSession happens in the HttpApplication, so I am working with the same session all the time.

I'll paraphrase the code here. It is a web application. A very simple page: a GridView with and ObjectDataSource. The gridview can be edited. The grid loads fine but when clicking Update (performing the UpdateMethog of the ObjectDataSource), there is an exception (see below). Here is the sequence of events when the pages loads to update the data:

1) First the data is loaded
public static IList GetInscriptionsFromCompetition(int ID)
{
if (ID <= 0)
return null;

Competition.Business.Competition ent = EntityDataSourceUtils.GetCompetition(ID);

if (ent == null)
return null;

return ent.Inscriptions;
}

Easy enough. The GetCompetition ends up calling Get(typeof(Competition), ID ) to load the object.

2) The Update event is processed and this function is called:

public static void UpdateCompetitionInscription(int ID, string PublicID, string Status)
{
CompetitionInscription ci = EntityDataSourceUtils.GetCompetitionInscription(ID);
if (ci == null)
return;

ci.PublicID = PublicID;
ci.Status = Status;

CompetitionInscriptionFacade fa = new CompetitionInscriptionFacade();
fa.Save(ci);
}


So this saves the data. The NHibernate call executed is:
ITransaction tx = null;

try
{
tx = m_session.BeginTransaction();
m_session.SaveOrUpdate(item);
tx.Commit();
}
catch (Exception ex)
{
if (tx != null) tx.Rollback();
throw ex;
}


3) After an update operation on the underlying datasource, the gridview refreshes the data with the same function as in 1). That is where I'm getting a problem (the exception happens elsewhere, but the problem is from here). In the first call to the GetInscriptionsFromCompetition function, the Inscriptions (it is an IList from a loaded by this definition:
<bag name="Inscriptions" inverse="true" >
<key column="CompetitionID"/>
<one-to-many class="Competition.Business.CompetitionInscription, Competition.Business"/>
</bag>

) contains 3 records, all of which are initialized. using the debugger and the object explorer, they all seem fine.

The second time that function is called however, the object that has just been saved is completly different that the other ones. It looks like an uninitialized proxy with _cache0 members and other stuff. Definitly not like the object normally looks. The other 2 objects are fine.

So the process of saving the object is what makes it become 'different'. I'm fairly new to NHibernate and I can't describe this much better without screenshots, but to me it looks like a proxy.

So, I tried to use NHibernateUtils.Ininitalize to force it to initialize properly. Unfortunatly that didn't do anything.

Full stack trace of any exception that occurs:
Here is the exception. It occurs when the gridview tries to bind the columns to the row DataItem. Since it is not the objects (as I said, it looks like a proxy) with the proper structure, the gridview can't find the proper Properties (well, if it were a proxy, it should have initilialized right then and there right? So maybe it is not a proxy). In any case, here is the exception. There are no nested exceptions:
TargetException: Object does not match target type.]
System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target) +2329733
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) +114
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +29
System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +17
System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object component) +151

[TargetInvocationException: Property accessor 'Status' on object 'Competition.Business.CompetitionInscription' threw the following exception:'Object does not match target type.']
System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object component) +498
System.Web.UI.WebControls.BoundField.GetValue(Control controlContainer) +428
System.Web.UI.WebControls.BoundField.OnDataBindField(Object sender, EventArgs e) +118
System.Web.UI.Control.OnDataBinding(EventArgs e) +88
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +167
System.Web.UI.Control.DataBind() +31
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.Control.DataBind() +31
System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +199
System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +4310
System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +89
System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +38
System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +126
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +98
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +154
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +99
System.Web.UI.WebControls.GridView.DataBind() +24
CompetitionManageInscriptionPage.gridInscriptions_RowUpdated(Object sender, EventArgs e) in c:\Projects\CompetitionDev3\Competition\WebSite\Competition.WebUI\CompetitionManageInscriptions.aspx.cs:56
System.Web.UI.WebControls.GridView.OnRowUpdated(GridViewUpdatedEventArgs e) +96
System.Web.UI.WebControls.GridView.HandleUpdateCallback(Int32 affectedRows, Exception ex) +111
System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +230
System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +1140
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +835
System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +162
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +118
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +107
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +175
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +31
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +32
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +244
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3835



Name and version of the database you are using:
SQL Server 2005 Express


Thanks in advance of any help provided.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 04, 2007 10:09 am 
Beginner
Beginner

Joined: Wed Mar 14, 2007 12:35 pm
Posts: 24
Use session.Evict on the object you just saved. When the datasource calls its databinding method again, everything will work fine.


Top
 Profile  
 
 Post subject: It works!
PostPosted: Wed Apr 04, 2007 10:30 am 
Newbie

Joined: Wed Apr 04, 2007 9:40 am
Posts: 12
Thanks, it works now. Should I put Evict() in my Save function as a standard practice? If not, in which case should it be used?

Regards,
Alain-Daniel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 04, 2007 12:02 pm 
Beginner
Beginner

Joined: Wed Mar 14, 2007 12:35 pm
Posts: 24
I've only ever had this issue in update scenarios.


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