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 Disconected objects
PostPosted: Tue Nov 04, 2008 5:12 pm 
Newbie

Joined: Tue Nov 04, 2008 5:02 pm
Posts: 4
I'm having some problems with NHibernate commiting my object changes back to the database. I have an object framework that utilizes NHibernate and the UI interacts with these objects via a WCF service, so I need to operate in a disconnected environment (since I don't want to manage the session on the individual objects). I have no problem retrieving data from the database but I can't seem to persist it back if I disconnect the object from the session and then try to reconnect it later (as a side note, i've tried committing to the database while in the same session and it does work so I know there's no problem with the mapping files etc). Here is the code I use to create the object:


Code:
public static T Get(int id)
        {
            using (ISessionFactory factory = new Configuration().Configure().BuildSessionFactory())
            {
                using (ISession session = factory.OpenSession())
                {
                    return (T)session.Load(typeof(T), id, LockMode.Upgrade);
                }
            }
        }



When I attempt to save the object, I use the following:

Code:
public virtual void Save()
        {
            if (this.ID != null)
                this.DateEdited = DateTime.Now;
            else
                this.DateAdded = DateTime.Now;

            using (ISessionFactory factory = new Configuration().Configure().BuildSessionFactory())
            {
                using (ISession session = factory.OpenSession())
                {
                    using (ITransaction transaction = session.BeginTransaction())
                    {
                        session.Lock(this, LockMode.None);
                        session.SaveOrUpdate(this);
                        transaction.Commit();
                    }
                    session.Flush();
                }
            }
        }



I don't get any errors when I save and it appears to work fine (no exceptions etc) but no data is actually saved back to the database. Can someone clue me in on what I'm missing? I've been over all the documentation and from what I can tell, I'm doing everything correctly. I've tried experimenting with different lockmodes but none of them seem to do the trick.

Thanks,

_________________
Shane A. McGarry
Senior Software Engineer


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 1:10 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
use merge() instead of first lock(), then saveOrUpdate().

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 11:09 am 
Newbie

Joined: Tue Nov 04, 2008 5:02 pm
Posts: 4
FYI - I had to eliminate the session.Lock() call and change the SaveOrUpdate to SaveOrUpdateCopy. The final code looks like this and works as expected:


Code:
public virtual void Save()
        {
            if (this.ID != null)
                this.DateEdited = DateTime.Now;
            else
                this.DateAdded = DateTime.Now;

            using (ISessionFactory factory = new Configuration().Configure().BuildSessionFactory())
            {
                using (ISession session = factory.OpenSession())
                {
                    using (ITransaction transaction = session.BeginTransaction())
                    {;
                        session.Merge(this);
                        session.SaveOrUpdateCopy(this);
                        transaction.Commit();
                    }
                    session.Flush();
                }
            }
        }



Thanks for the help!

_________________
Shane A. McGarry
Senior Software Engineer


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2008 7:10 am 
Newbie

Joined: Mon Nov 10, 2008 6:42 am
Posts: 1
Thanks for posting your solution. It solved also my
"NotUniqueObjectException: a different object with the same identifier value was already associated with the session".
My Dao-Code (C#, NHibernate 2.0 and Spring.NET 1.2.0 RC1)

Code:
public Art  SaveOrUpdate(Art art)
      {
         using ( ITransaction trx = sessionFactory.GetCurrentSession().BeginTransaction() )
         {
            sessionFactory.GetCurrentSession().Merge(art);
            sessionFactory.GetCurrentSession().SaveOrUpdateCopy(art);
            trx.Commit();      
         }
         sessionFactory.GetCurrentSession().Flush();
         return art;      
      }


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.