-->
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: Help with mapping
PostPosted: Tue Jan 03, 2006 11:47 am 
Regular
Regular

Joined: Tue Jan 03, 2006 11:43 am
Posts: 51
Location: Sweden
Hi,
I'm a beginner of NHibernate and need some help.
I have these database tables:

Nodes
---------
id:int
name:string

Connections
------------
scorecardId:int
nodeId:int


How would the xml look like if I want the class to look like this:

Node
-----------
ScorecardId:int
Children:IList
Name:string


Any help is appreciated!

/Carl


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 03, 2006 11:51 am 
Regular
Regular

Joined: Tue Jan 03, 2006 11:43 am
Posts: 51
Location: Sweden
Forgot one thing.. the Connections table should have a nodeIdParent.


Top
 Profile  
 
 Post subject: Re: Help with mapping
PostPosted: Tue Jan 03, 2006 4:47 pm 
Newbie

Joined: Tue Jan 03, 2006 4:42 pm
Posts: 1
Hey _carl_,

If you want to model it with two tables, it'll look something like this

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <!-- MULTI TABLE IMPLEMENTATION -->
  <class name="Test.Node, Test" table="Nodes">
    <id name="NodeId" type="System.Int32" unsaved-value="0">
      <generator class="native" />
    </id>
    <property name="Name" not-null="true"></property>
    <bag name="Children" table="Connections" lazy="true" inverse="true" cascade="save-update">
      <key column="NodeIdParent"></key>
      <one-to-many class="Test.Node, Test" ></one-to-many>
    </bag>
  </class>
</hibernate-mapping>



You could also do it with just a single table and do it like this...
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <!-- SINGLE TABLE IMPLEMENTATION -->
  <class name="Test.Node, Test" table="Nodes">
    <id name="NodeId" type="System.Int32" unsaved-value="0">
      <generator class="native" />
    </id>
    <property name="Name" not-null="true"></property>
    <many-to-one name="Parent" column="NodeIdParent" not-null="false" cascade="save-update"></many-to-one>
    <bag name="Children" lazy="true" inverse="true" cascade="save-update">
      <key column="NodeIdParent"></key>
      <one-to-many class="Test.Node, Test" ></one-to-many>
    </bag>
  </class>
</hibernate-mapping>


DISCLAIMER: This isn't mapping isn't tested...so there may be some bugs.

-Ben


_carl_ wrote:
Hi,
I'm a beginner of NHibernate and need some help.
I have these database tables:

Nodes
---------
id:int
name:string

Connections
------------
scorecardId:int
nodeId:int


How would the xml look like if I want the class to look like this:

Node
-----------
ScorecardId:int
Children:IList
Name:string


Any help is appreciated!

/Carl

_________________
http://blog.benday.com
http://www.benday.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 04, 2006 4:27 am 
Regular
Regular

Joined: Tue Jan 03, 2006 11:43 am
Posts: 51
Location: Sweden
Hi Ben and thanks for your answer!

I forgot one thing (again), the Connections table can contain several nodeId with the same value, but with different parents depending on in which scorecard they are in. So is there a way to have the class Node look something like this (pseudo):

Code:
<class name="Node" table="Connections">
<id ... /> // as before
<property name="Name" table="Nodes" />
<property name="Scorecard" />
<bag name="Children">
  <key column="NodeIdParent" />
  <one-to-many class="Node" />
</bag>
</class>


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.