Hi All,
I'm a relative newcomer to nhibernate and have just stumbled across an issue with the merge method for a detached entity containing a IDictionary <map> collection. Each time merge is called an Invalid cast exception is thown (System.InvalidCastException: Specified cast is not valid..): stack trace as follows.
Code:
ReplaceElements(Object original, Object target, Object owner, IDictionary copyCache, ISessionImplementor session)
NHibernate.Type.CollectionType.Replace(Object original, Object target, ISessionImplementor session, Object owner, IDictionary copyCache)
NHibernate.Type.TypeFactory.Replace(Object[] original, Object[] target, IType[] types, ISessionImplementor session, Object owner, IDictionary copiedAlready)
NHibernate.Event.Default.DefaultMergeEventListener.CopyValues(IEntityPersister persister, Object entity, Object target, ISessionImplementor source, IDictionary copyCache)
NHibernate.Event.Default.DefaultMergeEventListener.EntityIsDetached(MergeEvent event, IDictionary copyCache)
NHibernate.Event.Default.DefaultMergeEventListener.OnMerge(MergeEvent event, IDictionary copyCache)
NHibernate.Event.Default.DefaultMergeEventListener.OnMerge(MergeEvent event)
NHibernate.Impl.SessionImpl.FireMerge(MergeEvent event)
NHibernate.Impl.SessionImpl.Merge(String entityName, Object obj)
NHibernate.Impl.SessionImpl.Merge(Object obj)
A simplification of the mapping being used is:
Code:
<class name="Employee" table="Per_Employees" dynamic-update="true" lazy="false">
<!-- Identity mapping -->
<id name="Id">
<column name="EmployeeID" />
<generator class="native" />
</id>
<!-- one-to-many mapping: 'EmployeeAttributes' -->
<map name="EmployeeAttributes" table="Per_EmployeeAttributes" lazy="false">
<key column="EmployeeID" not-null="true" />
<index column="AttributeID" type="Int32" />
<element column="ValueID" type="Int32" />
</map>
</class>
And class:
Code:
public class Employee
{
private int _id;
private IDictionary<int, int> _employeeAttributes;
public virtual int Id
{
get { return _id; }
private set { _id = value; }
}
public virtual IDictionary<int, int> EmployeeAttributes
{
get { return _employeeAttributes; }
private set { _employeeAttributes = value; }
}
}
Using SaveOrUpdate seems to work but so far we're preferring Merge as dynamic updating doesn't seem to work with SaveOrUpdate with a detached architecture.
I've been searching around the internet for a day or so with no insight, so any guidance here would be hugely appreciated.
Thanks in advance,