469,889 Members | 1,233 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,889 developers. It's quick & easy.

NHibernate + Custom List: Unable to cast...


I'm using NHibernate 1.2 (CR1), and I'm using a custom list (inherited from
BindingList(Of T) ) for all my lists.

The NHibernate documentation told me that I had to implement
IUserCollectionType to my custom list, which I did. But I still get an error
when trying to read the object: "
{"Unable to cast object of type
'NHibernate.Collection.Generic.PersistentGenericBa g`1[Ghost.clsPersonne]' to
type 'BaseFramework.BaseList`1[Ghost.clsPersonne]'."}"

I looked at the sample here
but it didn't help a lot...

Does anybody knows what I'm doing wrong?

Thanks a lot in advance,


This is my Custom List:

Public Class BaseList(Of T As BaseClass)
Inherits BindingList(Of T)
Implements IComponent, IUserCollectionType


#Region "IUserCollectionType Implementation"

Public Function ReplaceElements(ByVal original As Object, ByVal target
As Object, ByVal persister As ICollectionPersister, ByVal owner As Object,
ByVal copyCache As System.Collections.IDictionary, ByVal session As
ISessionImplementor) As Object Implements
Dim Result As BaseList(Of T) = CType(target, BaseList(Of T))
For Each item As Object In CType(original, IEnumerable)
Result.Add(CType(item, T))
Return Result
End Function

Public Overloads Function IndexOf(ByVal collection As Object, ByVal
entity As Object) As Object Implements IUserCollectionType.IndexOf
Return CType(collection, BaseList(Of T)).IndexOf(CType(entity, T))
End Function

Public Function Instantiate() As Object Implements
Return New BaseList(Of T)()
End Function

Public Function Instantiate(ByVal session As ISessionImplementor, ByVal
persister As ICollectionPersister) As IPersistentCollection Implements
Return New PersistentIdentifierBag(Of T)(session)
End Function

Public Function Wrap(ByVal session As ISessionImplementor, ByVal
collection As Object) As IPersistentCollection Implements
Return New PersistentIdentifierBag(Of T)(session, CType(collection,
BaseList(Of T)))
End Function

Public Overloads Function Contains(ByVal collection As Object, ByVal
entity As Object) As Boolean Implements IUserCollectionType.Contains
Return CType(collection, BaseList(Of T)).Contains(CType(entity, T))
End Function

Public Function GetElements(ByVal collection As Object) As IEnumerable
Implements IUserCollectionType.GetElements
Return CType(collection, IEnumerable)
End Function

#End Region

End Class

These are my mapping files:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Ghost.clsEntreprise, Ghost" table="tblEntreprises"
dynamic-update="true" lazy="false">
<id name="EntrepriseID" column="EntrepriseID" type="Int32">
<generator class="identity" />
<property name="Nom" column="Nom" type="String" length="50" />
<bag name="Personnes" cascade="save-update" lazy="false">
<key column="EntrepriseID"/>
<one-to-many class="Ghost.clsPersonne, Ghost"/>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Ghost.clsPersonne, Ghost" table="tblPersonnes"
dynamic-update="true" lazy="false">
<id name="PersonneID" column="PersonneID" type="Int32" unsaved-value="0">
<generator class="identity" />
<property name="EntrepriseID" column="EntrepriseID" type="Int32"
length="4" />
<property name="Nom" column="Nom" type="String" length="50" />
<property name="Prenom" column="Prenom" type="String" length="50" />
<many-to-one name="Adresse" class="Ghost.clsAdresse, Ghost"
column="AdresseID" cascade="save-update" lazy="false"/>
Mar 30 '07 #1
0 3166

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by a | last post: by
reply views Thread by Aquila Deus | last post: by
1 post views Thread by erin.sebastian | last post: by
reply views Thread by beantaxi | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.