-->
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: Mapping multiple ID
PostPosted: Mon Jul 03, 2006 3:17 am 
Beginner
Beginner

Joined: Tue May 23, 2006 12:53 am
Posts: 34
I have a Customer class with SalesOrders and SalesOrder has SalesOrderItems which is an Item. Here's my code

Code:
/*
* Customer.cs 0.1
*
* Copyright 2006 Ian Escarro. All rights reserved.
* PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

using System;
using System.Collections;

namespace Talisay.Core
{
   /// <summary>
   /// Summary description for Customer.
   /// </summary>
   public class Customer
   {
. . .

      public IList Orders
      {
         get { return orders; }
      }

      public void AddOrder(SalesOrder order)
      {
         if (order == null)
            throw new NullReferenceException();
         order.Customer = this;
         orders.Add(order);
      }
   }
}


SalesOrder

Code:
/*
* SalesOrder.cs 0.1
*
* Copyright 2006 Ian Escarro. All rights reserved.
* PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

using System;
using System.Collections;

namespace Talisay.Core
{
   /// <summary>
   /// Summary description for SalesOrder.
   /// </summary>
   public class SalesOrder : IOrder
   {
      private int number;
      private DateTime date;
      private Customer customer;
      private IList items = new ArrayList();

      public SalesOrder() {}

      public int Number
      {
         get { return number; }
         set { number = value; }
      }

      public DateTime Date
      {
         get { return date; }
         set { date = value; }
      }

      public Customer Customer
      {
         get { return customer; }
         set { customer = value; }
      }

      public IList Items
      {
         get { return items; }
      }

      public void AddItem(SalesOrderItem o)
      {
         o.Order = this;
         items.Add(o);
      }
   }
}


SalesOrderItem

Code:
/*
* SalesOrderItem.cs 0.1
*
* Copyright 2006 Ian Escarro. All rights reserved.
* PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

using System;

namespace Talisay.Core
{
   /// <summary>
   /// Summary description for SalesOrderItem.
   /// </summary>
   public class SalesOrderItem : OrderItem
   {
      public SalesOrderItem(Item item, int quantity) : base(item, quantity) {}
      public SalesOrderItem() {}
   }
}


which is derived from

Code:
/*
* OrderItem.cs 0.1
*
* Copyright 2006 Ian Escarro. All rights reserved.
* PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

using System;

namespace Talisay.Core
{
   /// <summary>
   /// Summary description for OrderItem.
   /// </summary>
   public class OrderItem
   {
      private IOrder order;
      private Item item;
      private int quantity;

      public OrderItem(Item item, int quantity)
      {
         this.Item = item;
         this.Quantity = quantity;
      }

      public OrderItem() {}

      public IOrder Order
      {
         get { return order; }
         set { order = value; }
      }

      public Item Item
      {
         get { return item; }
         set { item = value; }
      }

      public int Quantity
      {
         get { return quantity; }
         set { quantity = value; }
      }
   }
}


I hope I don't confuse you guys. Thanks in advance!

[EDIT] and the mappings

Code:
<class name="Customer" table="Customers">
      <id name="Id" column="CustomerId">
         <generator class="identity"/>
      </id>
      <property name="CustomerName" column="CustomerName"/>
      <property name="Title" column="Title"/>
      <component name="Name" class="Name">
         <property name="LastName" column="LastName"/>
         <property name="FirstName" column="FirstName"/>
         <property name="MiddleName" column="MiddleName"/>
      </component>
      <bag name="Orders" cascade="all" access="nosetter.camelcase" lazy="false" inverse="true">
         <key column="CustomerId"/>
         <one-to-many class="SalesOrder"/>
      </bag>
   </class>


SalesOrder and SalesOrderItem

Code:
<class name="SalesOrder" table="SalesOrders">
      <id name="Number" column="Number" type="Int32">
         <generator class="identity"/>
      </id>
      <many-to-one name="Customer" column="CustomerId" class="Customer" cascade="none" />
   </class>
   
   <class name="SalesOrderItem" table="SalesOrderItems">
      <many-to-one name="Order" column="Number" class="SalesOrder" cascade="none" />
      <many-to-one name="Item" column="ItemCode" class="Item" cascade="none" />
      <property name="Quantity" column="Quantity"/>
   </class>


Can't seem to figure out 2 primary keys. The SalesOrder.Number and SalesOrder.ItemCode.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 4:27 am 
Beginner
Beginner

Joined: Tue May 23, 2006 12:53 am
Posts: 34
To view, it's something like this

Image


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 04, 2006 2:42 pm 
Regular
Regular

Joined: Tue Feb 07, 2006 4:27 pm
Posts: 69
If I am reading your model correct, you have a many-to-many relationship between your SaleOrders and Items class. This is facilitated via the intersection table SalesOrderItems.

In my application I have a similar structure therefore, I used a many-to-many mapping.

My suggestion to you would be to check out the many-to-many mapping and use that. It simplified your model and you don't even need an object for the SaleOrderItems table.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 04, 2006 5:12 pm 
Regular
Regular

Joined: Wed Jun 21, 2006 3:13 pm
Posts: 110
The many-to-many would work if it was purely a join table, but quantity has to be carried along as an attribute as well.

SalesOrderItems should be mapped something like this:
Code:
<class name="Talisay.Core.SalesOrderItem, Talisay" table="SalesOrderItems">
  <composite-id>
    <key-many-to-one name="Item" column="ItemCode" class="Talisay.Core.Item, Talisay"/>
    <key-many-to-one name="SalesOrder" column="Number" class="Talisay.Core.SalesOrder, Talisay"/>
  </composite-id>
  <property name="Quantity" column="quantity" />
</class>



Don't forget to override GetHashcode an Equals.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 04, 2006 9:55 pm 
Regular
Regular

Joined: Tue Feb 07, 2006 4:27 pm
Posts: 69
Sorry. I didn't see the quantity attribute on the join table. nice catch..


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 21, 2006 4:19 am 
Beginner
Beginner

Joined: Tue May 23, 2006 12:53 am
Posts: 34
I'm very sorry for the late reply but I can't seem to find a good way to start with this kind of relationship implementing in NHibernate. You see, I'm quite new to NHibernate and I don't see a good article for me. Any good working sample? Even with simple classes but with composite ids?

Sorry for the bother and thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 21, 2006 5:16 am 
Beginner
Beginner

Joined: Tue May 23, 2006 12:53 am
Posts: 34
Btw, these are my codes...

Code:
/*
* Customer.cs 0.1
*
* Copyright 2006 Ian Escarro. All rights reserved.
* PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

using System;
using System.Collections;

namespace NAccounting.Core.Sales
{
   /// <summary>
   /// Summary description for Customer.
   /// </summary>
   public class Customer
   {
      private int id;
.
.
.
      private IList orders = new ArrayList();

      public Customer() {}


Code:
/*
* SalesOrder.cs 0.1
*
* Copyright 2006 Ian Escarro. All rights reserved.
* PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

using System;
using System.Collections;
using NAccounting.Core.Inventory;

namespace NAccounting.Core.Sales
{
   /// <summary>
   /// Summary description for SalesOrder.
   /// </summary>
   public class SalesOrder
   {
      private int number;
      private Customer customer;
.
.
.
      private IList items = new ArrayList();

      public SalesOrder() {}


Code:
/*
* SalesOrderItem.cs 0.1
*
* Copyright 2006 Ian Escarro. All rights reserved.
* PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

using System;
using NAccounting.Core.Inventory;

namespace NAccounting.Core.Sales
{
   /// <summary>
   /// Summary description for SalesOrderItem.
   /// </summary>
   public class SalesOrderItem
   {
      private SalesOrder salesOrder;
      private Item item;
      private int quantity;

      public SalesOrderItem() {}


Code:
/*
* Item.cs 0.1
*
* Copyright 2006 Ian Escarro. All rights reserved.
* PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

using System;
using System.Collections;

namespace NAccounting.Core.Inventory
{
   /// <summary>
   /// Summary description for Item.
   /// </summary>
   public class Item
   {
      private int id;
.
.
.
      public Item() {}


and my mapping file

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
namespace="NAccounting.Core.Sales" assembly="NAccounting">
   <class name="Customer" table="Customers">
      <id name="Id" column="Id">
         <generator class="identity"/>
      </id>
      <property name="CustomerName" column="Name"/>
      <bag name="Orders" cascade="all" access="nosetter.camelcase" lazy="false" inverse="true">
         <key column="Id"/>
         <one-to-many class="SalesOrder"/>
      </bag>
   </class>
   <class name="SalesOrder" table="SalesOrders">
      <id name="Number" column="Number">
         <generator class="identity"/>
      </id>
      <property name="Date" column="Date"/>
      <many-to-one name="Customer" column="CustomerId" class="Customer" cascade="none"/>
      <bag name="Items" cascade="all" access="nosetter.camelcase" lazy="false" inverse="true">
         <key column="Number"/>
         <one-to-many class="SalesOrderItem"/>
      </bag>
   </class>
   <class name="SalesOrderItem" table="SalesOrderItems">
      <composite-id>
         <key-many-to-one name="Item" column="ItemId" class="NAccounting.Core.Inventory.Item, NAccounting"/>
         <key-many-to-one name="SalesOrder" column="Number" class="SalesOrder, NAccounting"/>
      </composite-id>
      <property name="Quantity" column="Quantity"/>
   </class>
</hibernate-mapping>


[EDIT] and the error I'm getting...

Code:
NHibernate: INSERT INTO Customers (Name) VALUES (@p0); select SCOPE_IDENTITY()
@p0 = 'foo'
NHibernate: INSERT INTO SalesOrders (CustomerId, Date) VALUES (@p0, @p1); select SCOPE_IDENTITY()
@p0 = '12'
@p1 = '7/21/2006 5:27:02 PM'
NHibernate: UPDATE SalesOrderItems SET Quantity = @p0 WHERE ItemId = @p1 AND Number = @p2
@p0 = '1'
@p1 = '1'
@p2 = '10'
TestCase 'NAccounting.Core.DataAccess.Tests.CustomerFacadeTest.TestSave'
failed: NHibernate.HibernateException : SQL insert, update or delete failed (expected affected row count: 1, actual affected row count: 0). Possible causes: the row was modified or deleted by another user, or a trigger is reporting misleading row count.
   at NHibernate.Impl.NonBatchingBatcher.AddToBatch(Int32 expectedRowCount)
   at NHibernate.Persister.EntityPersister.Update(Object id, Object[] fields, Object[] oldFields, Boolean[] includeProperty, Object oldVersion, Object obj, SqlString sqlUpdateString, ISessionImplementor session)
   at NHibernate.Persister.EntityPersister.Update(Object id, Object[] fields, Int32[] dirtyFields, Object[] oldFields, Object oldVersion, Object obj, ISessionImplementor session)
   at NHibernate.Impl.ScheduledUpdate.Execute()
   at NHibernate.Impl.SessionImpl.Execute(IExecutable executable)
   at NHibernate.Impl.SessionImpl.ExecuteAll(IList list)
   at NHibernate.Impl.SessionImpl.Execute()
   at NHibernate.Impl.SessionImpl.Flush()
   at NHibernate.Transaction.AdoTransaction.Commit()
   D:\ian\projects\csharp\naccounting\src\NAccounting\Core\DataAccess\CustomerFacade.cs(27,0): at NAccounting.Core.DataAccess.CustomerFacade.Save(Customer c)
   d:\ian\projects\csharp\naccounting\src\naccounting\core\dataaccess\tests\customerfacadetest.cs(30,0): at NAccounting.Core.DataAccess.Tests.CustomerFacadeTest.TestSave()

TestFixture failed:


0 passed, 1 failed, 0 skipped, took 3.05 seconds.


Help anyone? Millions of thanks in advance!


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.