-->
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: SaveOrUpdate not setting generated Id for MSSQL
PostPosted: Thu Apr 20, 2006 11:34 am 
Regular
Regular

Joined: Tue May 31, 2005 9:55 am
Posts: 67
Hibernate version: 1.0.2.0

I have a domain model in which I am calling ISession.SaveOrUpdate(entity) and the entity has a 1-M association to anoher entity with a native id (using MS SQL) that is configured to casacde.

When I use ISession.Save(entity), the entities Id property is assigned after the call the Save. However, when I update the association by adding a new entity and call ISession.SaveOrUpdate(entity), the associated entity Id property is not assigned and remaines at 0. Inspection in the database shows that the Id was properly assigned.

Is there something I am missing or is this possibly a bug.

Thanks,
craig


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 4:51 am 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
Post the mappings and the code, please.

Radu


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 9:16 am 
Regular
Regular

Joined: Tue May 31, 2005 9:55 am
Posts: 67
I have found the source of the problem, but am not sure if it is a bug in NHibernate or an implementation detail.

The following uses existing NHibernate domain model to demonstrate how I create new objects.

Code:
                    using( ISession s1 = OpenSession() )
                    using( ITransaction t1 = s1.BeginTransaction() )
                    {
                          M parent = new M();
                          N n = new N();
                          n.String = "0";
                          parent.AddChildren( n );

                          s1.Save( parent );

                          pid = Convert.ToInt64(s1.GetIdentifier(parent));
                          Assert.IsTrue(pid > 0);
                          Assert.IsTrue(Convert.ToInt64(s1.GetIdentifier(n)) > 0);               

                          t1.Commit();
                     }


This works fine. The following demonstrates how I would update and object model

Code:
                      using( ISession s2 = OpenSession() )
                      using( ITransaction t2 = s2.BeginTransaction() )
                      {
                            M parentLoaded = (M) s2.Load(typeof(M), pid);

                            N n = new N();
                            n.String = "P";
                            parentLoaded.AddChildren( n );

                            s2.SaveOrUpdate(parentLoaded);
                          [b]  s2.Flush();[/b]

                            Assert.IsTrue(Convert.ToInt64(s2.GetIdentifier(n)) > 0);               

                            t2.Commit();
                      }


If I do not include an ISession.Flush() before I access the generated identity property, the property will return the unsaved-value (0). However, with the Flush, it works. I only bring this up because the Flush is not required when creating a completely new object model and I am wondering if this a bug or required to simplify update implementation.

Thanks,
craig


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.