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
ClassesCode:
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; }
}
}