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...

Hi,

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
(http://analog-man.blogspot.com/2007/...ibernate.html),
but it didn't help a lot...

Does anybody knows what I'm doing wrong?

Thanks a lot in advance,

Pieter


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
IUserCollectionType.ReplaceElements
Dim Result As BaseList(Of T) = CType(target, BaseList(Of T))
Result.Clear()
For Each item As Object In CType(original, IEnumerable)
Result.Add(CType(item, T))
Next
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
IUserCollectionType.Instantiate
Return New BaseList(Of T)()
End Function

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

Public Function Wrap(ByVal session As ISessionImplementor, ByVal
collection As Object) As IPersistentCollection Implements
IUserCollectionType.Wrap
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" />
</id>
<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"/>
</bag>
</class>
</hibernate-mapping>

<?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" />
</id>
<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"/>
</class>
</hibernate-mapping>
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.