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.  [ 10 posts ] 
Author Message
 Post subject: IPersistentCollection cast exception
PostPosted: Wed May 19, 2010 3:53 am 
Newbie

Joined: Wed May 19, 2010 3:30 am
Posts: 6
Hi!

I have 2 classes:
first one - "ConstructionElement", here's the mapping:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Entities" namespace="Entities.Business">
    <class name="ConstructionElement" table="CNSTR_ELEM">
        <id name="Id" column="ID" type="Int64">
            <generator class="sequence">
       <param name="sequence">CNSTR_ELEM_SEQ</param>
    </generator>
        </id>

    <component name="MaterialDescription" class="Entities.Base.MultiNameEntity">
      <bag name="NamesI" table="CNSTR_ELEM_CD" lazy="false" cascade="all">
        <key column="PARENT_ID"/>
        <composite-element class="Entities.Localization.LocalizedString, Entities">
          <property column="CULTURE_ID" name="Language" type="Common.Enums.RequestedLanguage, Common"/>
          <property column="TEXT" name="Value" type="System.String"/>
        </composite-element>
      </bag>
    </component>
    </class>
</hibernate-mapping>


second - MultiNameEntity:

Code:
public class MultiNameEntity
{
        private ObservableCollection<LocalizedString> _names;
        private IList<LocalizedString> _namesI;
      
        protected virtual IList<LocalizedString> NamesI
        {
            get
            {
                if (_names != null)
                    return _names;
                return _namesI;
            }
            set { _namesI = value; }
        }

        public virtual ObservableCollection<LocalizedString> Names
        {
            get
            {
                if (_names == null)
                {
                    if (_namesI != null && NHibernateUtils.IsInitialized(_namesI))
                    {
                        _names = new ObservableCollection<LocalizedString>();
                        _names.AddRange(_namesI);
                    }
                }
                return _names;
            }
            set { _names = value; }
        }
}


When I'm trying to save instance of ConstructionElement, following exception occurs:
Code:
Unable to cast object of type 'System.Collections.ObjectModel.ObservableCollection`1[Entities.Localization.LocalizedString]' to type 'NHibernate.Collection.IPersistentCollection'.


If I'm bringing <bag> mapping out of <component> tag, then save\update works fine.


Top
 Profile  
 
 Post subject: Re: IPersistentCollection cast exception
PostPosted: Thu May 20, 2010 1:51 am 
Newbie

Joined: Wed May 19, 2010 3:30 am
Posts: 6
Any ideas? Really need it to work this way...


Top
 Profile  
 
 Post subject: Re: IPersistentCollection cast exception
PostPosted: Thu May 20, 2010 3:02 am 
Beginner
Beginner

Joined: Fri Feb 27, 2009 6:07 am
Posts: 38
Location: Moscow,Russia
Show code ConstructionElement and write version NHibernate.


Top
 Profile  
 
 Post subject: Re: IPersistentCollection cast exception
PostPosted: Thu May 20, 2010 3:25 am 
Newbie

Joined: Wed May 19, 2010 3:30 am
Posts: 6
ConstructionElement
Code:

public class ConstructionElement : Entity
{
   public virtual MultiNameEntity MaterialDescription { get; set; }      
}   


Entity

Code:
public abstract class Entity : IEntity
{
   public virtual long Id {get; set;}
}


Nhibernate 2.1.0.4000


Top
 Profile  
 
 Post subject: Re: IPersistentCollection cast exception
PostPosted: Thu May 20, 2010 3:48 am 
Beginner
Beginner

Joined: Fri Feb 27, 2009 6:07 am
Posts: 38
Location: Moscow,Russia
ObservableCollection Supported in: .NET Framework 3.5, 3.0 SP1, 3.0
Nhibernate 2.1.0.4000 base on .NET Framework 2.0.


Top
 Profile  
 
 Post subject: Re: IPersistentCollection cast exception
PostPosted: Thu May 20, 2010 3:55 am 
Newbie

Joined: Wed May 19, 2010 3:30 am
Posts: 6
Replacing ObservableCollection<> with List<> generates the same exception, but I got List, istead of ObservableCollection in exception message.

Code:
Unable to cast object of type 'System.Collections.Generic.List`1[Entities.Localization.LocalizedString]' to type 'NHibernate.Collection.IPersistentCollection'.


And NH maps to NamesI - which is IList<>


Top
 Profile  
 
 Post subject: Re: IPersistentCollection cast exception
PostPosted: Thu May 20, 2010 4:16 am 
Beginner
Beginner

Joined: Fri Feb 27, 2009 6:07 am
Posts: 38
Location: Moscow,Russia
Check type property NamesI and type items before invoke session .Save().


Top
 Profile  
 
 Post subject: Re: IPersistentCollection cast exception
PostPosted: Thu May 20, 2010 4:30 am 
Newbie

Joined: Wed May 19, 2010 3:30 am
Posts: 6
4 sure (MaterialDescription.NamesI).GetType() returns ObservableCollection, cause _names not null.


Top
 Profile  
 
 Post subject: Re: IPersistentCollection cast exception
PostPosted: Thu May 20, 2010 4:46 am 
Beginner
Beginner

Joined: Fri Feb 27, 2009 6:07 am
Posts: 38
Location: Moscow,Russia
I test your sample.
Code:
       ITransaction transaction=session.BeginTransaction();
       ConstructionElement ce = new ConstructionElement();
        MultiNameEntity me =new MultiNameEntity();
        LocalizedString ls = new LocalizedString();
        ls.Language = Language.English;
        ls.Text = "test";
        me.Names = new List<LocalizedString>();
        me.Names.Add(ls);
        ce.MaterialDescription = me;
        session.Save(ce);
        transaction.Commit();

all works fine.


Top
 Profile  
 
 Post subject: Re: IPersistentCollection cast exception
PostPosted: Thu May 20, 2010 4:55 am 
Newbie

Joined: Wed May 19, 2010 3:30 am
Posts: 6
Damn! this exception maddens me for 2 days :(
It works on select, but fails on save\update...
Thx, anyway...


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