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.  [ 2 posts ] 
Author Message
 Post subject: bidirectional one-to-many with an indexed collection
PostPosted: Sun Dec 31, 2006 3:24 am 
Beginner
Beginner

Joined: Thu Nov 03, 2005 1:52 am
Posts: 21
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 classes

Code:
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 schema

Code:
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
)


Problem

I 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 message

Code:
[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


Last edited by FrankWang on Tue Jan 02, 2007 7:32 pm, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 02, 2007 8:19 am 
Beginner
Beginner

Joined: Thu Nov 03, 2005 1:52 am
Posts: 21
Found the answer:

NHibernate does not support bidirectional one-to-many associations with an indexed collection (list, map or array) as the "many" end, you have to use a set or bag mapping.

See the following doc: http://www.hibernate.org/hib_docs/nhibernate/html/collections.html

The behavior will be:

When inverse is false at "many" end, as show in the example, the children's index columns have the correct values, but you can not delete parent and its children.

When inverse is true at "many" end, the children's index columns are always equal to 0 and you can only delete parent and its first child

Hope this will save others some headache.

Frank


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