-->
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: 2 questions: Update parentcolumn and return identity
PostPosted: Thu Jan 24, 2008 9:43 am 
Beginner
Beginner

Joined: Mon Jan 14, 2008 10:58 am
Posts: 24
I'm sorry but I don't understand! :(
Map file:
Code:
<class name="Swh.Entities.Category, Swh.Entities" table="Categories">
      <id name="Id" column="CategoryId" type="int" unsaved-value="0">
         <generator class="identity" />
      </id>
      <many-to-one name="Parent" column="ParentId" />   
      <property name="Name" column="CategoryName" type="String"/>
   </class>


Pretty simple, one category that has a parent category.


Question 1:
Now I want to make an update like this:
Code:
...
ITransaction transaction = CurrentSession.BeginTransaction();
Category cat = new Category();
cat.Id = 12345;
cat.Name = "test";
cat.ParentId = 54321; [b]How should this line look like?[/b]
//cat.Parent.Id = 54321;
CurrentSession.SaveOrUpdate(cat);
transaction.Commit();

The column in the database is ParentId.
Can't I use the Parent relation mapping for this? Do I have to add a property named ParentId to my map?


Question 2:
How do I return the primary key from an category insert?
Code:
ITransaction transaction = CurrentSession.BeginTransaction();
Category cat = new Category();
cat.Name = "test2";
//cat.ParentId = 54321;
int Id = CurrentSession.SaveOrUpdate(cat); [b]How should this line look like?[/b]
transaction.Commit();


I guess stupid simple questions but I can't find the answers in other threads!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 24, 2008 10:16 am 
Beginner
Beginner

Joined: Mon Jan 14, 2008 10:58 am
Posts: 24
Hmm, one solved, one to go!

Returning identity was (as I thought) simple!
Code:
ITransaction transaction = CurrentSession.BeginTransaction();
Category cat = new Category();
cat.Name = "test2";
CurrentSession.Save(cat);
transaction.Commit();
int id = cat.Id;

NHibernate adds the id automaticly for me to my class! :)

So, what about updating the parentid? How do I solve that?
I tried to add a property field to the map file like
Code:
<class name="Swh.Entities.Category, Swh.Entities" table="Categories">
      <id name="Id" column="CategoryId" type="int" unsaved-value="0">
         <generator class="identity" />
      </id>
      <many-to-one name="Parent" column="ParentId" />   
      <property name="ParentId" column="ParentId" type="int"/>
      <property name="Name" column="CategoryName" type="String"/>
   </class>

But then I just get an error saying I got duplicate columns! :(

I also tried this:
Code:
ITransaction transaction = CurrentSession.BeginTransaction();
Category cat = new Category();
cat.Id = 12345;
cat.Name = "test";
cat.Parent.Id = 54321;
CurrentSession.SaveOrUpdate(cat);
transaction.Commit();

But then I just got "Object reference not set to an instance of an object" on the cat.Parent.Id line.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 24, 2008 10:35 am 
Beginner
Beginner

Joined: Sun Nov 18, 2007 10:39 am
Posts: 46
Location: Liverpool, England
Fetch the parent object, then assign it to the parent property in the child


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 25, 2008 6:57 am 
Beginner
Beginner

Joined: Mon Jan 14, 2008 10:58 am
Posts: 24
Ahh, thanks kev! :)


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.