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 question
PostPosted: Tue Nov 15, 2005 2:31 am 
Newbie

Joined: Fri Oct 28, 2005 8:23 pm
Posts: 12
hi,
ive been doing alot of reading on the parent child assocations and from everything i have read it doesnt seem possible to automatically create the assocation of the parent in the child record.

Unless I have the two lines in the code to associate the newInvoiceLineItemOne.invoice = newInvoice i cant get the id to insert to the invoice line item table.

Is this required?

Is there a cleaner way to do this?


here is an example of what I am trying to do

Code:
Invoice newInvoice = new Invoice();

InvoiceLineItem newInvoiceLineItemOne = new InvoiceLineItem();
newInvoiceLineItemOne.description = "description one";

InvoiceLineItem newInvoiceLineItemTwo = new InvoiceLineItem();
newInvoiceLineItemTwo.description = "description two";

newInvoice.invoiceLineItems.Add( newInvoiceLineItemOne );
newInvoice.invoiceLineItems.Add( newInvoiceLineItemTwo );

newInvoiceLineItemOne.invoice = newInvoice;  //shouldnt be necessary??
newInvoiceLineItemTwo.invoice = newInvoice; //shouldnt be necessary??

Db.Save(newInvoice);



here are my mappings

Code:
   <class name="SS.Invoice, SS" table="INVOICE" discriminator-value="?">
    <id name="invoiceId" column="INVOICE_ID" type="Guid" unsaved-value="00000000-0000-0000-0000-000000000000">
    <generator class="guid" />
    </id>
      
    <bag name="invoiceLineItems"  lazy="true" inverse="true" cascade="all">
        <key column="INVOICE_ID" />
        <one-to-many class="SS.InvoiceLineItem, SS" />
    </bag>
</class>


<class name="SS.InvoiceLineItem, SS" table="INVOICE_LINE_ITEM" discriminator-value="?">
    <id name="invoiceLineItemId" column="INVOICE_LINE_ITEM_ID" type="Guid" unsaved-value="00000000-0000-0000-0000-000000000000">
        <generator class="guid" />
    </id>
    <many-to-one name="invoice" column="INVOICE_ID" class="SS.Invoice, SS" cascade="none" />
    <property name="description" column="DESCRIPTION" type="String" />
      
</class>




edit: for clarity

As always, thanks! for your feedback.

Sean


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 11:25 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
Code:
newInvoiceLineItemOne.invoice = newInvoice;

is required because of inverse="true".

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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 8:59 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
We're trying strongly typed collections to avoid having to explicitly assign the parent to a child after adding it to the parent's collection (as well as making sure someone doesn't try to add the wrong type to a collection).

Since NHibernate doesn't like strongly typed collections, we map the parent's collection property using field naming strategy, where the field is of type IList or whatever. Then our public property is of the strongly typed collection, which wraps the weakly typed collection that NHibernate manages.

The strongly typed collection has properties called Owner and ItemOwnerPropertyName. In the parent's getter for the collection property, when creating the strongly typed collection wrapper on first access, it passes itself and the known property name that the child type uses to refer back to its parent.

Then, in the strongly typed collection's IList.Add implementation, if Owner and ItemOwnerPropertyName are set, it uses reflection to set the parent property on the child.


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.