Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
NHibernate 1.2 beta 2
Two mapping files
Code:
<class name="EBSimple.Commerce.DomainModel.Product, EBSimple.Commerce.DomainModel" table="EBS_C_Product">
<list name="Images" table="EBS_C_ProductImage" lazy="true" cascade="all-delete-orphan" inverse="false">
<key column="ProductId"/>
<index column="SortOrder"/>
<one-to-many class="EBSimple.Commerce.DomainModel.ProductImage, EBSimple.Commerce.DomainModel"/>
</list>
</class>
Code:
<class name="EBSimple.Commerce.DomainModel.ProductImage, EBSimple.Commerce.DomainModel" table="EBS_C_ProductImage">
<property name="SortOrder" column="SortOrder"/>
<many-to-one name="Product" class="EBSimple.Commerce.DomainModel.Product, EBSimple.Commerce.DomainModel" column="ProductId"/>
</class>
Two Persistent classesCode:
public class Product : DomainBaseObject<String>
{
private IList<ProductImage> _Images = new List<ProductImage>();
public virtual IList<ProductImage> Images
{
get { return _Images; }
set { _Images = value; }
}
}
Code:
public class ProductImage : DomainBaseObject<String>
{
protected int _SortOrder;
protected Product _Product;
public virtual int SortOrder
{
get { return _SortOrder; }
set { _SortOrder = value; }
}
public virtual Product Product
{
get { return _Product; }
set { _Product= value; }
}
}
Two table schemaCode:
CREATE TABLE [dbo].[EBS_C_Product] (
[Id] [uniqueidentifier] NOT NULL ,
[ProductName] [nvarchar] (255) not null)
.
.
.
)
CREATE TABLE [dbo].[EBS_C_ProductImage] (
[Id] [uniqueidentifier] NOT NULL ,
[ProductId] [uniqueidentifier] NOT NULL ,
[ImageFile] [nvarchar] (255) NOT NULL ,
[UpdatedBy] [int] NOT NULL ,
[UpdatedDate] [datetime] NOT NULL ,
[SortOrder] [int] NOT NULL
)
ProblemI can save product (Parent )and product images (children) to database using the following code:
Code:
Product objShoes = DaoFactory.GetProductDao().GetById(objProduct.Id, false);
ProductImage image1 = new ProductImage();
ProductImage image2 = new ProductImage();
image2.Product = objShoes;
image1.Product = objShoes;
objShoes.Images.Add(image1);
objShoes.Images.Add(image2);
NHibernateSessionManager.Instance.BeginTransaction();
_productDao.Save(objShoes);
NHibernateSessionManager.Instance.CommitTransaction();[/i]
But I can't delete product and its children using the following code:Code:
NHibernateSessionManager.Instance.BeginTransaction();
_productDao.Delete(objShoes);
NHibernateSessionManager.Instance.CommitTransaction();
It failed on last line of code.
Error messageCode:
[i]{"could not delete collection: [EBSimple.Commerce.DomainModel.Product.Images#ea5f22ed-1e10-4675-a009-98a8012af071]"}
[NHibernate.ADOException]: {"could not delete collection: [EBSimple.Commerce.DomainModel.Product.Images#ea5f22ed-1e10-4675-a009-98a8012af071]"}
base {System.ApplicationException}: {"could not delete collection: [EBSimple.Commerce.DomainModel.Product.Images#ea5f22ed-1e10-4675-a009-98a8012af071]"}[/i]
inner exception is:Code:
{"Cannot insert the value NULL into column 'SortOrder', table 'Northwind.dbo.EBS_C_ProductImage'; column does not allow nulls. UPDATE fails.\r\nThe statement has been terminated."} System.Exception {System.Data.SqlClient.SqlException}
Question One
I was trying to delete a product and its children, why did NHibernate generate insert statement? What I did wrong?
Question Two
From I read in JIRA, I suspect this problem is related to NH-681. Can anybody confirmed this?
Thank you so much for your input and Happy new year