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.