-->
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: Parent/child mapping file with only one database table
PostPosted: Mon Jan 14, 2008 11:07 am 
Beginner
Beginner

Joined: Mon Jan 14, 2008 10:58 am
Posts: 24
Hi everyone! :)

This is my mapping file as it is now:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="Entities.Category, Entities" table="Categories">
      <id name="Id" column="CategoryID" type="int" unsaved-value="0">
         <generator class="identity" />
      </id>
      <property name="ParentId" column="ParentID" type="int"/>
      <property name="Name" column="CategoryName" type="String" length="50"/>
      <property name="CreateDate" type="DateTime"/>
      <property name="LastUpdatedDate" type="DateTime"/>
   </class>
</hibernate-mapping>


How should it be changed so ParentId has a relation to Id?

I have seen many examples with parent/child mappings but all of them has two tables and therefor also two classes.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 14, 2008 11:41 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
With the given mapping, you are only mapping an INT column to an Int32 property; there is no relationship to speak of as far as NHibernate is concern. That said, NHibernate has no problem mapping the relationship that you desire: change the <property> to <many-to-one> like this:
Code:
<many-to-one name="Parent" column="ParentID" />

Also rename the ParentID property to "Parent" and change its type from "int" to "Category".

Read to the documentation for <many-to-one> to fully understand it:
http://www.hibernate.org/hib_docs/nhibe ... -manytoone

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 14, 2008 1:15 pm 
Beginner
Beginner

Joined: Mon Jan 14, 2008 10:58 am
Posts: 24
So thats how its done! Thanks Karl! :)


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.