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: composite-element many-to-one not working
PostPosted: Wed Nov 21, 2007 2:20 am 
Newbie

Joined: Wed Nov 21, 2007 2:03 am
Posts: 1
I come across a strange problem where I just can't get it right no matter how I mappped the mapping files to C# class.

I have a following tables:
Code:
CREATE TABLE [dbo].[product](
   [id_product] [int] IDENTITY(1,1) NOT NULL,
   [name] [varchar](max)
)

Code:
CREATE TABLE [dbo].[product_order](
   [id_po] [int] NOT NULL,
   [pk_order] [int] NOT NULL,
   [pk_product] [int] NOT NULL,
   [quantity] [int] NOT NULL
)


Code:
CREATE TABLE [dbo].[order](
   [id_order] [int] IDENTITY(1,1) NOT NULL,
   [description] [varchar](max) NOT NULL
)


Basically, [product_order] is a many-many table where pk_order and pk_product as the composite keys.

I have the following hbm mapping files for [order]

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="OrderTest" assembly="OrderTest">
  <class name="Order" table="order" lazy="false">
    <id name="id_order" column="id_order" type="System.Int32">
      <generator class="native"/>
    </id>
    <property name="description" column="description" type="System.String"/>
    <set name="ProductOrder" table="product_order" lazy="false" inverse="false">
      <key column="pk_order" />
      <composite-element class="Product">
        <property name="quantity" column="quantity" type="System.Int32"/>
        <many-to-one name="Product" class="Product" column="pk_product" />
      </composite-element>
    </set>
  </class>
</hibernate-mapping>


... and for [product] ...

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="OrderTest" assembly="OrderTest">
  <class name="Product" table="product" lazy="false">
    <id name="id_product" column="id_product" type="System.Int32">
      <generator class="native"/>
    </id>
    <property name="name" column="name" type="System.String"/>
  </class>
</hibernate-mapping>


The corresponded file for c# are

Code:
//------------------------------------------------------------------------------
// <autogenerated>
//     This code was generated by NHibernate.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </autogenerated>
//------------------------------------------------------------------------------

using System;
using Iesi.Collections;

namespace OrderTest
{
   /// <summary>
   /// POJO for Order. This class is autogenerated
   /// </summary>
   [Serializable]
   public  class Order
   {
      #region Fields
      
      private Int32 _id_order;
      private String _description;
      private Iesi.Collections.ISet productOrder;

      #endregion

      #region Constructors
      
      /// <summary>
      /// Initializes a new instance of the Order class
      /// </summary>
      public Order()
      {
      }
   
      /// <summary>
      /// Initializes a new instance of the Order class
      /// </summary>
      /// <param name="_description">Initial <see cref="Order.description" /> value</param>
      /// <param name="productOrder">Initial <see cref="Order.ProductOrder" /> value</param>
      public Order(String _description, Iesi.Collections.ISet productOrder)
      {
         this._description = _description;
         this.productOrder = productOrder;
      }
   
      /// <summary>
      /// Minimal constructor for class Order
      /// </summary>
      /// <param name="productOrder">Initial <see cref="Order.ProductOrder" /> value</param>
      public Order(Iesi.Collections.ISet productOrder)
      {
         this.productOrder = productOrder;
      }
      #endregion
   
      #region Properties
      
      /// <summary>
      /// Gets or sets the id_order for the current Order
      /// </summary>
      public Int32 id_order
      {
         get { return this._id_order; }
         set { this._id_order = value; }
      }
      
      /// <summary>
      /// Gets or sets the description for the current Order
      /// </summary>
      public String description
      {
         get { return this._description; }
         set { this._description = value; }
      }
      
      /// <summary>
      /// Gets or sets the ProductOrder for the current Order
      /// </summary>
      public Iesi.Collections.ISet ProductOrder
      {
         get { return this.productOrder; }
         set { this.productOrder = value; }
      }
      
      #endregion
   }
}

//------------------------------------------------------------------------------
// <autogenerated>
//     This code was generated by NHibernate.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </autogenerated>
//------------------------------------------------------------------------------

using System;

namespace OrderTest
{
   /// <summary>
   /// POJO for Product. This class is autogenerated
   /// </summary>
   [Serializable]
   public  class Product
   {
      #region Fields
      
      private Int32 _id_product;
      private String _name;

      #endregion

      #region Constructors
      
      /// <summary>
      /// Initializes a new instance of the Product class
      /// </summary>
      public Product()
      {
      }
   
      /// <summary>
      /// Initializes a new instance of the Product class
      /// </summary>
      /// <param name="_name">Initial <see cref="Product.name" /> value</param>
      public Product(String _name)
      {
         this._name = _name;
      }
   
      #endregion
   
      #region Properties
      
      /// <summary>
      /// Gets or sets the id_product for the current Product
      /// </summary>
      public Int32 id_product
      {
         get { return this._id_product; }
         set { this._id_product = value; }
      }
      
      /// <summary>
      /// Gets or sets the name for the current Product
      /// </summary>
      public String name
      {
         get { return this._name; }
         set { this._name = value; }
      }
      
      #endregion
   }
}

//------------------------------------------------------------------------------
// <autogenerated>
//     This code was generated by NHibernate.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </autogenerated>
//------------------------------------------------------------------------------

using System;

namespace OrderTest
{
   /// <summary>
   /// POJO for Product. This class is autogenerated
   /// </summary>
   [Serializable]
   public class ProductOrder
   {
      #region Fields
      
      private Int32 _quantity;
      private Product product;

      #endregion

      #region Constructors
      
      /// <summary>
      /// Initializes a new instance of the Product class
      /// </summary>
      public ProductOrder()
      {
      }
   
      /// <summary>
      /// Initializes a new instance of the Product class
      /// </summary>
      /// <param name="_quantity">Initial <see cref="Product.quantity" /> value</param>
      /// <param name="product">Initial <see cref="Product.Product" /> value</param>
      public ProductOrder(Int32 _quantity, Product product)
      {
         this._quantity = _quantity;
         this.product = product;
      }
   
      #endregion
   
      #region Properties
      
      /// <summary>
      /// Gets or sets the quantity for the current Product
      /// </summary>
      public Int32 quantity
      {
         get { return this._quantity; }
         set { this._quantity = value; }
      }
      
      public Product Product
      {
         get { return this.product; }
         set { this.product = value; }
      }
      
      #endregion
   }
}

I have no problem building the project, but Vs2005 makes noise when I try to run it:

Could not find a setter for property 'quantity' in class 'OrderTest.Product' at NHibernate.Property.BasicPropertyAccessor.GetSetter(Type type, String propertyName) at NHibernate.Mapping.Property.GetSetter(Type clazz) at NHibernate.Cfg.HbmBinder.BindComponent(XmlNode node, Component model, Type reflectedClass, String className, String path, Boolean isNullable, Mappings mappings) at NHibernate.Cfg.HbmBinder.BindCollectionSecondPass(XmlNode node, Collection model, IDictionary persistentClasses, Mappings mappings) at NHibernate.Cfg.HbmBinder.BindSetSecondPass(XmlNode node, Set model, IDictionary persistentClasses, Mappings mappings) at NHibernate.Cfg.HbmBinder.SetSecondPass.SecondPass(IDictionary persistentClasses) at NHibernate.Cfg.CollectionSecondPass.DoSecondPass(IDictionary persistentClasses) at NHibernate.Cfg.Configuration.SecondPassCompile() at NHibernate.Cfg.Configuration.BuildSessionFactory() at OrderTest._Default.Page_Load(Object sender, EventArgs e) in C:\Documents and Settings\Bong\My Documents\Visual Studio 2005\Projects\OrderTest\Default.aspx.cs:line 29


I wonder what was wrong with my code and mapping? I have been trying for 2 days with any success.

BTW, I use nhibernate 1.2.0.GA and MS SQL2005
Thank in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 21, 2007 10:56 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
NHibernate cannot find the property "quantity" in the "Product" class. You need another class that plays the role of an association class for the <set> you have mapped in the Order class. In NHibernate, you cannot add any extra information to a many-to-many association (such as quantity in your case). What you need are two many-to-one associations with the association class in between. The "quantity" would hang off this association class.

_________________
Karl Chu


Top
 Profile  
 
 Post subject: Re: composite-element many-to-one not working
PostPosted: Wed Nov 21, 2007 11:19 am 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
Well, that's what ProductOrder is doing, but the mapping is incorrect. The <composite-element> class should be ProductOrder, not Product:

Code:
      <composite-element class="ProductOrder">
        <property name="quantity" column="quantity" type="System.Int32"/>
        <many-to-one name="Product" class="Product" column="pk_product" />
      </composite-element>


HTH!


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.