-->
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.  [ 1 post ] 
Author Message
 Post subject: Confusion about mapping string collections
PostPosted: Tue May 13, 2008 10:41 pm 
Newbie

Joined: Tue May 13, 2008 10:25 pm
Posts: 2
Greetings, I am trying to map some existing classes to a database. One class that I am trying to map has a string collection that I want to store. The class uses a custom StringCollection class that I cannot change. StringCollection derives from System.Collections.CollectionsBase, therefore implements IList, ICollection and IEnumerable.

I am trying to figure out how to map this string list to the database. I have boiled it down to a very simple example. I am trying to use the <List> type, but I get an exception telling me it can't cast NHibernate.Collections.PersistentList to my collection. This makes sense... but is there something I can do here to use my collection instead???

Thanks,
Brian

Hibernate version:
1.2.1

Mapping documents:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Testing.SimpleTest, NHibernate.Test" table="SimpleObjects" lazy="false" >
    <id name="Name" column="SimpleName" type="String">
      <generator class="assigned" />
    </id>
    <component name="MyInnerClass">
      <property name="InnerName" column="InnerName" />
      <list name="MyStrings" table="InnerStrings">
        <key column="SimpleName" />
        <index column="CollectionIndex" type="Int32"/>
        <element column="InnerValue" type="String" />
      </list>
    </component>
  </class>
</hibernate-mapping>



Code between sessionFactory.openSession() and session.close():
Code:
            ITransaction transaction = _session.BeginTransaction();

            Testing.SimpleTest simple = new Testing.SimpleTest();
            simple.Name = "MySimpleName";
            simple.MyInnerClass.InnerName = "MyInnerName";
            simple.MyInnerClass.MyStrings.Add("STRING123");
            simple.MyInnerClass.MyStrings.Add("STRING345");

            _session.Save(simple);
            transaction.Commit();


Full stack trace of any exception that occurs:
Quote:
NHibernate.MappingException : Invalid mapping information specified for type Testing.InnerClass, check your mapping file for property type mismatches
----> System.InvalidCastException : Unable to cast object of type 'NHibernate.Collection.PersistentList' to type 'MyApp.StringCollection'.
at NHibernate.Type.ComponentType.SetPropertyValues(Object component, Object[] values)
at NHibernate.Impl.WrapVisitor.ProcessComponent(Object component, IAbstractComponentType componentType)
at NHibernate.Impl.AbstractVisitor.ProcessValue(Object value, IType type)
at NHibernate.Impl.WrapVisitor.ProcessValues(Object[] values, IType[] types)
at NHibernate.Impl.SessionImpl.DoSave(Object theObj, EntityKey key, IEntityPersister persister, Boolean replicate, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)
at NHibernate.Impl.SessionImpl.DoSave(Object obj, Object id, IEntityPersister persister, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)
at NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything)
at NHibernate.Impl.SessionImpl.Save(Object obj)
Q:\Report\DefinitionDatabase\NHibernate\NHibernate\Hibernate.cs(47,0): at NHibernate.Hibernate.StoreSomething()
Q:\Report\DefinitionDatabase\NHibernate\NHibernate\TestNHibernate.cs(42,0): at NHibernate.TestNHibernate.InitializeHibernate()
--InvalidCastException
at (Object , Object[] , SetterCallback )
at NHibernate.Bytecode.Lightweight.AccessOptimizer.SetPropertyValues(Object target, Object[] values)
at NHibernate.Type.ComponentType.SetPropertyValues(Object component, Object[] values)


Name and version of the database you are using:
SQlite v3

Classes

Code:
    public class SimpleTest
    {
        private string _name = string.Empty;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private InnerClass _innerClass = new InnerClass();
        public InnerClass MyInnerClass
        {
            get { return _innerClass; }
            set { _innerClass = value; }
        }
    }

    public class InnerClass
    {
        private string _innerName = string.Empty;
        public string InnerName
        {
            get { return _innerName; }
            set { _innerName = value; }
        }

        private StringCollection _myStrings = new StringCollection();
        public StringCollection MyStrings
        {
            get { return _myStrings; }
            set { _myStrings = value; }
        }
    }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.