-->
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.  [ 2 posts ] 
Author Message
 Post subject: NHibernate Collection Mapping vb.net
PostPosted: Wed Apr 12, 2006 7:50 am 
Newbie

Joined: Wed Apr 12, 2006 7:12 am
Posts: 1
Hello, I have just started getting to gribs with NHibernate.

I have created a Parent / Child relationship, and I am able to successfully Add to the children collection so long as the parent exists.

Perhaps my understanding is wrong but I was under the impression it is possible to create a new parent object, then create a number of child objects, then save the whole lot to the database by just saving the Parent.
However when I try to add a child object, I get an error saying that a reference has not been set.

I have gone back to something simple, to get working first.

Simple XML Mapping
<class name="Parent" table="ARM_PARENT" discriminator-value="C">
<id name="Id" column="nId" type="Int32" unsaved-value="0">
generator class="identity"/>
</id>
<property name="Text" type="String" column="sText"/>

<bag name="Children" cascade="all-delete-orphan">
<key column="nParentId" />
<one-to-many class="Child" />
</bag>
</class>

<class name="Child" table="ARM_CHILD" discriminator-value="C">
<id name="Id" column="nId" type="Int32" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="Text" type="String" column="sText"/>
<property name="ParentId" type="Int32" column="nParentId"/>
</class>

Simple code
Dim objNHibernate As New _NHibernate
Dim MyParent As New Parent
Dim MyChild As New Child

MyParent.Text = "Flub"
MyChild.Text = "xxxx"
MyParent.Children.Add(MyChild)
objNHibernate.Session.Save(MyParent)
objNHibernate.Session.Flush()

In my class Children is IList

I don't really understand the difference between using "bag" and "Set" in the mapping file either. :(

I would be grateful if you could help me get this concept straightened out. Thanks Stewart


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 19, 2006 6:00 am 
Newbie

Joined: Wed Feb 01, 2006 10:45 am
Posts: 18
Location: Salzburg, Austria
hi Stewart,

a BAG is unsorted and may contain multiple instances of a row
a SET (like HashSet) has exactly one row for a certain ID

As Hibernate is not a managed container you have to tell the child
who his parents are:

this should work:
Code:
Dim objNHibernate As New _NHibernate
Dim MyParent As New Parent
Dim MyChild As New Child

MyParent.Text = "Flub"
MyChild.Text = "xxxx"

MyChild.Parent = MyParent

MyParent.Children.Add(MyChild)
objNHibernate.Session.Save(MyParent)
objNHibernate.Session.Flush()

_________________
greetings,
kris


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