-->
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: Duplicated Objects
PostPosted: Fri Sep 17, 2010 4:55 am 
Newbie

Joined: Mon Jul 26, 2010 2:25 am
Posts: 6
I was playing around with NHibernate 3 Alpha 2 and created a simple Model.
I've a Customer, with a List of Orders, which are Mapped to Products by OrderLine.

Now when I'm loading a Customer, I get duplicate Orders (one for every OrderLine it has).

The Mapping (simplified):
Code:
  <class name="Customer" table="Customer" >
    <id name="Id" type="long" unsaved-value="0">
      <generator class="native" />
    </id>

    {Properties omitted}

    <bag name="Orders" cascade="all-delete-orphan" inverse="true" lazy="true">
      <key column="CustomerId" not-null="true" on-delete="cascade"/>
      <one-to-many class="Order"/>
    </bag>
  </class>

  <class name="Order" table="[Order]" lazy="false">
    <id name="Id" type="long" unsaved-value="0">
      <generator class="native" />
    </id>

    {Properties omitted}

    <many-to-one name="OrderedBy" class="Customer" column="CustomerId" />

    <bag name="OrderLines" cascade="all-delete-orphan" inverse="true" lazy="true">
      <key column="OrderId" not-null="true" on-delete="cascade"/>
      <one-to-many class="OrderLine"/>
    </bag>
  </class>

  <class name="OrderLine" table="OrderLine" >
    <composite-id name="Id" class="OrderLineKey">
      <key-many-to-one name="Order" column="OrderId" lazy="proxy" class="Order"/>
      <key-many-to-one name="Product" column="ProductId" lazy="proxy" class="Product"/>
    </composite-id>

    {Properties omitted}
  </class>

  <class name="Product" table="Product" lazy="false">
    <id name="Id" type="long" unsaved-value="0">
      <generator class="native" />
    </id>

    {Properties omitted}
  </class>


OrderLineKey Class:
Code:
    public class OrderLineKey {
        public virtual Order Order { get; set; }
        public virtual Product Product { get; set; }

        {GetHashCode/Equals Omitted}
    }


This is the loading:
Code:
this.Session.QueryOver<Customer>()
                .Where(e => e.Id == customerId)
                .Fetch(f => f.Orders).Eager
                .Fetch(f => f.Orders.First().OrderLines).Eager
                .SingleOrDefault()


I don't know if there is a conceptual error in my model or mapping, any advice would be appreciated.


Top
 Profile  
 
 Post subject: Re: Duplicated Objects
PostPosted: Fri Sep 17, 2010 6:05 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
http://nhforge.org/wikis/howtonh/get-unique-results-from-joined-queries.aspx

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Duplicated Objects
PostPosted: Fri Sep 17, 2010 6:14 am 
Newbie

Joined: Mon Jul 26, 2010 2:25 am
Posts: 6
Tried the DistinctRootEntityResultTransformer, but this does not work on the nested objects.


Top
 Profile  
 
 Post subject: Re: Duplicated Objects
PostPosted: Fri Sep 17, 2010 7:32 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Hmm, you can try and map the orders as <set> or <list> which both do not allow duplicate entries. Then only one of each item will be added

_________________
--Wolfgang


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.