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.  [ 5 posts ] 
Author Message
 Post subject: About the Order-LineItem-Product example
PostPosted: Thu Mar 05, 2009 2:26 pm 
Newbie

Joined: Tue Jul 29, 2008 10:53 am
Posts: 11
In the documenation there's an example of a many-to-many relation. But it misses the C# objects.

http://www.hibernate.org/hib_docs/reference/en/html/example-mappings-customerorderproduct.html

Has anyone made this example work?

I've tried it with these C# objects:

Code:
public class Order
{
   public Int64 ID { get; set; }
   public Int CustomerID { get; set; }
   public DateTime Timestamp { get; set; }
   public IList<LineItem> LineItems { get; set; }
}


public class LineItem
{
   public Int64 ID { get; set; }
   public Int OrderID { get; set; }
   public Product OrderdProduct { get; set; }
}

public class Product
{
   public Int64 ID { get; set; }
   public String SerialNumber { get; set; }
}


But somehow I get like thousands of lineItems which are all null, even when I have only a few LineItems in my database.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 06, 2009 3:16 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
What line_number values do you have in the table ? If they do not start with 0 or if some are missingm (e.g 0,4,5,6,9, ...) , you'll get empty entries in the list.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 06, 2009 6:14 am 
Newbie

Joined: Tue Jul 29, 2008 10:53 am
Posts: 11
I've fixed the problem!

This was the original mapping:

Code:
<class name="Order" table="orders">

     <id name="id">
         <generator class="native"/>
     </id>

     <property name="date"/>

     <many-to-one name="customer" column="customer_id"/>

     <list name="lineItems" table="line_items">
         <key column="order_id"/>

         <list-index column="line_number"/>

         <composite-element class="LineItem">
             <property name="quantity"/>
             <many-to-one name="product" column="product_id"/>
         </composite-element>
     </list>

</class>



The problem is that the LineItems are mapped as a List. This doesn't work very well. When you change this to a set, and remove the list-index part, everything works fine:

Code:
<class name="Order" table="orders">

     <id name="id">
         <generator class="native"/>
     </id>

     <property name="date"/>

     <many-to-one name="customer" column="customer_id"/>

     <set name="lineItems" table="line_items">
         <key column="order_id"/>

         <composite-element class="LineItem">
             <property name="quantity"/>
             <many-to-one name="product" column="product_id"/>
         </composite-element>
     </set>

</class>



And this is how the C# object is coded:

public class Order
{
public Int64 ID { get; set; }
public Int CustomerID { get; set; }
public DateTime Timestamp { get; set; }
public Iesi.Collections.Generic.ISet<LineItem> LineItems { get; set; }
}


public class LineItem
{
public Int64 ID { get; set; }
public Int OrderID { get; set; }
public Product OrderdProduct { get; set; }
}

public class Product
{
public Int64 ID { get; set; }
public String SerialNumber { get; set; }
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 06, 2009 6:20 am 
Newbie

Joined: Tue Jul 29, 2008 10:53 am
Posts: 11
I've fixed the problem!

This was the original mapping:

Code:
<class name="Order" table="orders">

     <id name="id">
         <generator class="native"/>
     </id>

     <property name="date"/>

     <many-to-one name="customer" column="customer_id"/>

     <list name="lineItems" table="line_items">
         <key column="order_id"/>

         <list-index column="line_number"/>

         <composite-element class="LineItem">
             <property name="quantity"/>
             <many-to-one name="product" column="product_id"/>
         </composite-element>
     </list>

</class>



The problem is that the LineItems are mapped as a List. This doesn't work very well. When you change this to a set, and remove the list-index part, everything works fine:

Code:
<class name="Order" table="orders">

     <id name="id">
         <generator class="native"/>
     </id>

     <property name="date"/>

     <many-to-one name="customer" column="customer_id"/>

     <set name="lineItems" table="line_items">
         <key column="order_id"/>

         <composite-element class="LineItem">
             <property name="quantity"/>
             <many-to-one name="product" column="product_id"/>
         </composite-element>
     </set>

</class>



And this is how the C# object is coded:

public class Order
{
public Int64 ID { get; set; }
public Int CustomerID { get; set; }
public DateTime Timestamp { get; set; }
public Iesi.Collections.Generic.ISet<LineItem> LineItems { get; set; }
}


public class LineItem
{
public Int64 ID { get; set; }
public Int OrderID { get; set; }
public Product OrderdProduct { get; set; }
}

public class Product
{
public Int64 ID { get; set; }
public String SerialNumber { get; set; }
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 06, 2009 6:21 am 
Newbie

Joined: Tue Jul 29, 2008 10:53 am
Posts: 11
I've fixed the problem!

This was the original mapping:

Code:
<class name="Order" table="orders">

     <id name="id">
         <generator class="native"/>
     </id>

     <property name="date"/>

     <many-to-one name="customer" column="customer_id"/>

     <list name="lineItems" table="line_items">
         <key column="order_id"/>

         <list-index column="line_number"/>

         <composite-element class="LineItem">
             <property name="quantity"/>
             <many-to-one name="product" column="product_id"/>
         </composite-element>
     </list>

</class>



The problem is that the LineItems are mapped as a List. This doesn't work very well. When you change this to a set, and remove the list-index part, everything works fine:

Code:
<class name="Order" table="orders">

     <id name="id">
         <generator class="native"/>
     </id>

     <property name="date"/>

     <many-to-one name="customer" column="customer_id"/>

     <set name="lineItems" table="line_items">
         <key column="order_id"/>

         <composite-element class="LineItem">
             <property name="quantity"/>
             <many-to-one name="product" column="product_id"/>
         </composite-element>
     </set>

</class>



And this is how the C# object is coded:

public class Order
{
public Int64 ID { get; set; }
public Int CustomerID { get; set; }
public DateTime Timestamp { get; set; }
public Iesi.Collections.Generic.ISet<LineItem> LineItems { get; set; }
}


public class LineItem
{
public Int64 ID { get; set; }
public Int OrderID { get; set; }
public Product OrderdProduct { get; set; }
}

public class Product
{
public Int64 ID { get; set; }
public String SerialNumber { get; set; }
}


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