-->
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.  [ 13 posts ] 
Author Message
 Post subject: returning an ID when saving a new object
PostPosted: Wed May 18, 2005 8:03 am 
How do I return an ID when saving a new object.

e.g. contentId = dbMgr.Save(myContent);

where contentId is the newly created primary key of the saved content.


Top
  
 
 Post subject:
PostPosted: Wed May 18, 2005 8:55 am 
Newbie

Joined: Fri May 13, 2005 1:59 pm
Posts: 11
Assuming you are using an identity column (auto-incrementing) as you primary key, after calling Save, your object's ID member will be set to the value of the identity column for the newly inserted row.

e.g.

Code:
dbMgr.Save(myContent);
contentId = myContent.Id;


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 18, 2005 9:20 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
ISession.Save also returns the new object's id.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 18, 2005 9:48 am 
thx to you both


Top
  
 
 Post subject:
PostPosted: Wed May 18, 2005 10:08 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
If you use SaveOrUpdate(), you have to extract it from the entity:
Code:
myEntity = mySession.SaveOrUpdate(myEntity);
return myEntity.ID;


You should take the entity returned by mySession because, if they are in different AppDomains (happens when using Remoting/WebService) the first entity will not be the updated one (and its ID will still be 0).

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject: for some reason this isn't working properly
PostPosted: Mon May 23, 2005 1:06 pm 
Hi there,

We're trying to use it this way, but for some reason when we do:

Code:
myContent = new Content();
dbMgr.saveContent(myContent);
iRecId = myContent.ContentId;


...we are getting back a myContent.ContentId of 0.

We have also tried explicitly returning an int from the dbMgr.SaveContent(mycontent), and using:

Code:
myContent = new Content();
iRecId = dbMgr.saveContent(myContent);


...but this results also in a 0 being returned. The database table is auto-incrementing using an identity and a primary key (it is SQL Server 2000).


Top
  
 
 Post subject: actually...
PostPosted: Mon May 23, 2005 1:27 pm 
Hm, it's something we're doing wrong. This code:
Code:
public void saveContent(Content c)
      {
         ISession session = null;
         ITransaction tx = null;
         try
         {
            session = factory.OpenSession();
            tx = session.BeginTransaction();
            session.SaveOrUpdate(c);
            tx.Commit();
            session.Close();
         }
         catch (Exception ex)
         {
            tx.Rollback();
            // handle exception
            System.Diagnostics.Debug.WriteLine("exception: " + ex);
         }
         finally
         {
            session.Close();
         }
      }


results in an exception being thrown:

"could not synchronize database state with session"

We must be screwing up our class mappings or something?


Top
  
 
 Post subject:
PostPosted: Tue May 24, 2005 3:37 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
srimedia, can you post the mapping of the Content class? And the dbMgr.saveContent method too.

Also, it's impossible to tell what the problem is from that exception. Inner exceptions contain all the useful information.


Top
 Profile  
 
 Post subject: code
PostPosted: Tue May 24, 2005 7:24 am 
Hi Sergey, here is the SaveContent code,

Code:

public void saveContent(Content c)
      {
         ISession session = null;
         ITransaction tx = null;
         try
         {
         session = factory.OpenSession();
         tx = session.BeginTransaction();
         session.SaveOrUpdate(c);
         tx.Commit();
         session.Close();
                      }
         catch (Exception ex)
         {
            tx.Rollback();
            // handle exception
            System.Diagnostics.Debug.WriteLine("exception: " + ex);
         }
         finally
         {
            session.Close();
         }
      }



And here is the mapping code...

Code:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="MyProject.Content, MyProject" table="content">
      <id name="ContentId" column="content_id" type="Int32" unsaved-value="-1">
         <generator class="native" />
      </id>
      <property name="Title" column="title" />
      <property name="Summary" column="summary" />
      <property name="StartDate" column="start_date" />
      <property name="EndDate" column="end_date" />
      <property name="Active" column="active" />
      <property name="ContentTypeId" column="content_type_id" />
      <bag name="Links">
      <key column="content_id"/>
      <one-to-many class="MyProject.Link, MyProject"/>
      </bag>
      <bag name="Images" table="image">
      <key column="image_id"/>
      <composite-element class="MyProject.Image, MyProject">
      <property name="Filename" column="filename" />
      <property name="Title" column="title"/>
      </composite-element>
      </bag>
      <bag name="ContentTexts">
      <key column="content_id"/>
      <one-to-many class="MyProject.ContentText, MyProject"/>
      </bag>
      
      
   </class>
   

</hibernate-mapping>




And the inner exception is
{"SQL update or deletion failed (row not found)" }

Thanks for your help!


Top
  
 
 Post subject:
PostPosted: Tue May 24, 2005 8:21 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Well, you have unsaved-value specified as "-1", but from your previous posts it looks like you are using 0 by default. Try initializing the ContentId to -1 in the Content class and see what happens.

(By the way, you don't really have to use SaveOrUpdate, Save should work just fine.)


Top
 Profile  
 
 Post subject: thanks!
PostPosted: Tue May 24, 2005 11:26 am 
Hey, Sergey,

Thanks a lot for your help, that did the trick!

We've seen quite a few different things about Save, Update, and SaveOrUpdate, we'll take a crack with just using Save. Is there any advantage to this other than less typing?

Greets from London,
srimedia


Top
  
 
 Post subject:
PostPosted: Tue May 24, 2005 1:43 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
SaveOrUpdate does either Save or Update :) depending on the unsaved-value of identifier (and possibly version). It's useful when you don't know which method to call and want to let NHibernate decide.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 7:00 am 
Heheheh. :D Ok, we should continue to use SaveOrUpdate in that code then, it's not apparent from the context of what we gave you but we are using that code for both Save and Update functionality.

Thanks very much again for your help with this,
srimedia


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