OK --- IT WORKS with this particular implementation of property:

    public class Private
    {
        public Category[] PrivateCategory { get; set; }

        private IList<Category> _PrivateCategoryList = null;

        public IList<Category> PrivateCategories
        {
            get
            {
                if (_PrivateCategoryList == null)
                {
                    _PrivateCategoryList = new
List<Category>(PrivateCategory);
                }

                return _PrivateCategoryList;
            }
            set
            {
                _PrivateCategoryList = value;

                if (_PrivateCategoryList != null)
                {
                    PrivateCategory = new
Category[_PrivateCategoryList.Count];
                    for (int i = 0; i < _PrivateCategoryList.Count; i+
+)
                    {
                        PrivateCategory[i] = _PrivateCategoryList[i];
                    }
                }
            }
        }
    }

Can you tell me why, it works?
One critical step is " _PrivateCategoryList = value;" in the setter...
may be ..

But tell me what you think ..

This was cracked in 7 hrs ... ;)

On May 24, 11:47 am, sando <[email protected]> wrote:
> Hello Ricardo,
>
> thanks for dropping by the question .... Alas, it still does not work :
> (
>
> I tried to find if I am returning List instead of IList, but I checked
> everything is fine, still nhibernate throws the sameexception.
>
> I again re-iterate here, that if I move the component outside and
> flatten the class/mapping, the same code works fine.
> But it is not a feasible solution in our context. Could you please
> read the code below, and point me where I am doing wrong.
>
> I am pasting a sample code to replicate the problem:
>
> ----- STOCK CLASS ----
> public class Stock
>     {
>         public int? stockId { get; set; }
>         public string stockCode { get; set; }
>         public string stockName { get; set; }
>         public IList<Category> PublicCategory { get; set; }
>         public Private Private { get; set; }
>     }
>
>     public class Private
>     {
>         public Category[] PrivateCategory { get; set; }
>
>         // This is required as I want to map the array as a <bag>
> because our data model does not have index column
>         private IList<Category> _PrivateCategoryList = null;
>
>         // This property is only for the mapping and the "private
> IList<Category> _PrivateCategoryList" gets filled when nhibernate
> calls the property
>         public IList<Category> PrivateCategories
>         {
>             get
>             {
>                 if (_PrivateCategoryList == null)
>                 {
>                     _PrivateCategoryList = new List<Category>();
>
>                     foreach (Category c in PrivateCategory)
>                     {
>                         _PrivateCategoryList.Add(c);
>                     }
>                 }
>
>                 return _PrivateCategoryList;
>             }
>             set
>             {
>                 if (value != null)
>                 {
>                     PrivateCategory = new Category[value.Count];
>                     for (int i = 0; i < value.Count; i++)
>                     {
>                         PrivateCategory[i] = value[i];
>                     }
>                 }
>             }
>         }
>     }
>
> ----- STOCK MAPPING ------
>
>   <class name="Stock" table="Stock" lazy="false">
>     <id name="stockId" column="STOCK_ID">
>       <generator class="native" />
>     </id>
>     <property name="stockCode" column="STOCK_CODE"/>
>     <property name="stockName" column="STOCK_NAME"/>
>
>           <bag name="PublicCategory" table="Stock_Category" cascade="all-
> delete-orphan" lazy="false" where="TYPE=0">
>                   <key column="STOCK_ID"/>
>                   <many-to-many class="Category" column="CATEGORY_ID"/>
>                   <sql-insert>
>                           INSERT INTO Stock_Category(Type, STOCK_ID, 
> CATEGORY_ID)
> values(0,?,?)
>                   </sql-insert>
>           </bag>
>
>           <component name ="Private" class="Private">
>                   <bag name="PrivateCategories" table="Stock_Category" 
> cascade="all-
> delete-orphan" lazy="false" where="TYPE=1">
>                           <key column="STOCK_ID"/>
>                           <many-to-many class="Category" 
> column="CATEGORY_ID"/>
>                           <sql-insert>
>                                   INSERT INTO Stock_Category(Type, STOCK_ID, 
> CATEGORY_ID)
> values(1,?,?)
>                           </sql-insert>
>                   </bag>
>           </component>
>
>   </class>
>
> ---- CATEGORY CLASS ----------
>     public class Category
>     {
>         public int? categoryId { get; set; }
>         public string categoryName { get; set; }
>     }
>
> ---- CATEGORY MAPPING ------------
>   <class name="Category" table="Category" lazy="false">
>     <id name="categoryId" column="CATEGORY_ID">
>       <generator class="native" />
>     </id>
>     <property name="categoryName" column="CATEGORY_NAME"/>
>
>   </class>
>
> -
> sando
>
> On May 23, 10:20 pm, Ricardo Peres <[email protected]> wrote:
>
>
>
>
>
>
>
> > That's because in your code you have a List<Something> instead of a
> > IList<Something> (notice the I).
>
> > On May 23, 12:27 pm, sando <[email protected]> wrote:
>
> > > Please read the detail of my problem, which is exactly similar 
> > > to:http://www.mail-archive.com/[email protected]/msg18775.html
>
> > > Unable to cast object of type:
> > > 'System.Collections.Generic.List`1[ENTITY]' to type
> > > 'NHibernate.Collection.IPersistentCollection'.
>
> > > However, my properties return IList<T> and NOT List<T>.
>
> > > To summarize: I have a <bag> inside a <component> as nhibernate
> > > mapping.
> > > I always get a castexception.
>
> > > If I'm bringing <bag> mapping out of <component> tag, then save\update
> > > works fine.
>
> > > Is this a bug in nhibernate?

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to