-->
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: TreeView, TreeNodePopulate and NHibernate
PostPosted: Tue Apr 25, 2006 8:39 am 
Newbie

Joined: Tue Apr 04, 2006 4:47 am
Posts: 18
Hi,

I am working on a sample page where I want my page to setup my table and then fill my database with some starter data. After this root nodes will be added to a TreeView and using the TreeNodePopulate event further queries will be sent to the database and the tree updated. I also have a button to remake the database tables and repopulate the treeview with rootnodes.

For this I use the well known NHibernateHttpModule by Benday to setup the factory and provide sessions.

The strange thing is that the expanding of the treeview goes fine, the resetting of the database too. But when I expand a root node and thereafter resetting the database it complains about a unique key contraint violation. This leads me to believe that somehow retrieving child nodes of an expanding node causes a database reset on the next request to not actually go through causing:

Code:
NHibernate.NonUniqueObjectException was unhandled by user code
  Message="a different object with the same identifier value was already associated with the session: 1, of class: Symtech.Concept.Logic.BusinessUnit"
  Source="NHibernate"
  StackTrace:
       at NHibernate.Impl.SessionImpl.CheckUniqueness(Key key, Object obj)
       at NHibernate.Impl.SessionImpl.DoSave(Object theObj, Key key, IClassPersister persister, Boolean replicate, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)
       at NHibernate.Impl.SessionImpl.DoSave(Object obj, Object id, IClassPersister persister, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)
       at NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything)
       at NHibernate.Impl.SessionImpl.Save(Object obj)
       at NHibernate.Impl.SessionImpl.SaveOrUpdate(Object obj)
       at Symtech.Concept.DataAccess.BusinessUnitHandler.Save(BusinessUnit bu) in D:\projecten\Symtech.Concept\DataAccess\BusinessUnitHandler.cs:line 45
       at Symtech.Concept.DataAccess.SchemaCreator.CreateSchema() in D:\projecten\Symtech.Concept\DataAccess\SchemaCreator.cs:line 25
       at _Default.btnSchema_Click(Object sender, EventArgs e) in d:\projecten\Symtech.Concept\WebUI\Default.aspx.cs:line 49
       at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
       at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


The code executed:
Code:
        public static void CreateSchema()
        {
            SchemaUtility.ExportSchema();
            BusinessUnit root = new BusinessUnit();
            root.Name = "Symphony Technology";
            BusinessUnit dev = new BusinessUnit();
            dev.Name = "Sofware Development";
            root.AddDevision(dev);
            BusinessUnit ond = new BusinessUnit();
            ond.Name = "Customer Support";
            root.AddDevision(ond);
            BusinessUnit sen = new BusinessUnit();
            sen.Name = "System Engineering";
            root.AddDevision(sen);
            BusinessUnitHandler.Save(root);
        }


Once again this code works fine untill I expand a node before it causing this to run:

Code:
    protected void tvwBusinessUnits_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
        TreeNode parent = e.Node;
       
        // Children ophalen van deze treenode
        IList children = BusinessUnitHandler.Devisions(int.Parse(parent.Value));

        // Node vullen
        foreach (BusinessUnit bu in children)
        {
            TreeNode tn = new TreeNode();
            tn.PopulateOnDemand = true;
            tn.Text = bu.Name;
            tn.Value = bu.Id.ToString();
            parent.ChildNodes.Add(tn);
        }
    }


Does anyone have the slightest idea why this would go wrong? It looks like something causes the generation of a wrong Id, but expanding a node as the cause seems so weird to me...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 9:45 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
If You do not create new session after recreating database schema:

1. The session's first level cache contains the objects loaded by session.
2. The Id counter from database is resetted, so id-s of new objects clash with id-s in session cache.

Gert


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 10:38 am 
Newbie

Joined: Tue Apr 04, 2006 4:47 am
Posts: 18
Thanks!

Since the original NHibernateHttpModule doesn't have a setter for CurrentSession I have added the following function:

Code:
        public static ISession SessionRefresh()
        {
            if (HttpContext.Current == null)
            {
                // running without an HttpContext (non-web mode)
                // the nhibernate session is a singleton in the app domain
                m_session = CreateSession();
                return m_session;
            }
            else
            {
                // running inside of an HttpContext (web mode)
                // the nhibernate session is a singleton to the http request
                HttpContext currentContext = HttpContext.Current;
                ISession session;

                session = CreateSession();
                currentContext.Items[KEY_NHIBERNATE_SESSION] = session;
                return session;
            }
        }


Invokign this after exporting the schema made it work like a charm! Thanks a lot.


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.