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.  [ 7 posts ] 
Author Message
 Post subject: Generics - Mapping Collections Example Please
PostPosted: Tue Aug 15, 2006 3:42 pm 
Regular
Regular

Joined: Tue Feb 07, 2006 4:27 pm
Posts: 69
I'm looking for a simple example of using a generic collection to map a one-to-many association using NHibernate 1.2.0 alpha 1.

What I Have now

Code:
    public class Invoice
    {
        private IList _LineItems;
    }


What I would like

Code:
    public class Invoice
    {
        private IList<LineItem> _LineItems;
    }


Any help would be great, as I'm not finding good documenation on this feature since it has been integrated.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 15, 2006 5:00 pm 
Newbie

Joined: Tue Aug 01, 2006 9:42 pm
Posts: 6
Code:
    class Invoice
    {
        private IList<LineItem> _LineItems;

        public Invoice()
        {
            this._LineItems = new List<LineItem>();
        }

        public IList<LineItem> LineItems
        {
            get { return this._LineItems; }
            set { this._LineItems = value; }
        }
    }


Code:
    class LineItem
    {
        private Invoice _Invoice;

        public LineItem()
        {
        }

        public Invoice ParentInvoice
        {
            get { return this._Invoice; }
            set { this._Invoice = value; }
        }
    }


Code:
<class name="YourNamespace.Invoice, YourAssembly" table="Invoices">
  ...
  <bag generic="true" name="LineItems" inverse="true" cascade="all-delete-orphan">
    <key column="InvoiceID"></key>
    <one-to-many class="YourNamespace.LineItem, YourAssembly"/>
  </bag>
</class>



Code:
<class name="YourNamespace.LineItem, YourAssembly" table="LineItems">
  ...
  <many-to-one
  name="Invoice"
  column="InvoiceID"
  class="YourNamespace.LineItem, YourAssembly" />
</class>



This should give you the right idea. Don't forget, just like any other bidirectional collection association you must maintain both sides in your object. ie


Code:
LineItem li = new LineItem();
Invoice1.LineItems.Add(li);
li.ParentInvoice = Invoice1;


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 15, 2006 6:08 pm 
Regular
Regular

Joined: Tue Feb 07, 2006 4:27 pm
Posts: 69
Excellent. I have mapped these types of relationship before, I had no idea it was as easy as generic="true" to use generics with them.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 16, 2006 2:20 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
gcook1@shaw.ca wrote:
Excellent. I have mapped these types of relationship before, I had no idea it was as easy as generic="true" to use generics with them.

Thanks


AFAIK, You should not need the generic="true"... They should work just "as is", use old mapping but the new generic IList<>. Did You have any problems with mapping generics?

Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 16, 2006 7:45 am 
Regular
Regular

Joined: Tue Feb 07, 2006 4:27 pm
Posts: 69
So far things are working well. I've got the object mapped but I have yet to develop my mapping tests for it so I'm not using it right now.

NHibernate was happy with how it is mapped for now. I'll keep your suggestion in mind though if my mapping test for this association fails once I get the test developed.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 16, 2006 8:13 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
NHibernate doesn't need generic="true", you can switch between generic and non-generic types without altering the mapping files.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 06, 2006 8:13 pm 
Regular
Regular

Joined: Wed Oct 25, 2006 10:51 pm
Posts: 71
Code:
<class name="YourNamespace.Invoice, YourAssembly" table="Invoices">
  ...
  <bag generic="true" name="LineItems" inverse="true" cascade="all-delete-orphan">
    <key column="InvoiceID"></key>
    <one-to-many class="YourNamespace.LineItem, YourAssembly"/>
  </bag>
</class>


Hi Aaron,

I was wondering why you use a 'bag' collection type. Just semantically speaking, since we're using a 'List' I would have thought you would use a <list> ?

Admittedly, while I'm looking at the documentation on collections (beta2), it's not yet clear to me what the difference is, or really exactly what a bag is.

But you can ignore that, I'm just hoping there's a sensible reason why you need something other than a <list> for a List.


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