-->
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.  [ 9 posts ] 
Author Message
 Post subject: Custom List: Unable to cast object of type ...
PostPosted: Mon Apr 02, 2007 3:52 am 
Beginner
Beginner

Joined: Mon Mar 26, 2007 5:47 am
Posts: 22
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.PersistentGenericBag`1[Ghost.clsPersonne]' to
type 'BaseFramework.BaseList`1[Ghost.clsPersonne]'."}"

I looked at the sample here
(http://analog-man.blogspot.com/2007/01/ ... rnate.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>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 02, 2007 4:17 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
First, the IUserCollectionType should be implemented by a separate object, not by your collection class. Second, you should of course let NHibernate know about the custom type using collection-type attribute in the mapping file:

Code:
<bag name="..." collection-type="MyUserCollectionType, MyAssembly">


Top
 Profile  
 
 Post subject: 'TestMyList' is not a member of 'System.Collections.IList
PostPosted: Tue Apr 03, 2007 7:59 am 
Beginner
Beginner

Joined: Mon Mar 26, 2007 5:47 am
Posts: 22
Hi Sergey,

Thanks for the answer. It doesn't give any exception anymore, but still doesn't have the behaviour that I wanted to... So I tested with the sample provided in the Hibernate.Test, and even that sample doesn't behave like I would expect.

I simply added a function to MyList, but I'm never able to use that function (''TestMyList' is not a member of 'System.Collections.IList'.)!!!! I added one to MyListType and PersistentMylist, and I can't access them either :-S

So actually I'm kind of in doubt if these UserCollectionTypes actually work? When I want to use my UserCollection, I must be at least be able to access the Methods on that UserCollection, but this solution doesn't allow me to do it????


Please tell me what is wrong: If I am doing something wrong, or did I misunderstood the purpose of IUserCollectionType?? For now, I don't have any clue.. :-/

If you want to I can send you some code...

A desesperating Pieter :-)



My modified MyList:
-----------------------
Imports System
Imports System.Collections
Imports System.Text

Namespace NHibernate.Test.UserCollection
Public Class MyList
Inherits ArrayList

Public Function TestMyList() As String 'added these 3 lines
Return "Count = " & Me.Count 'added these 3 lines
End Function 'added these 3 lines

End Class
End Namespace


My btnTest_Click:
---------------------
Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
Dim u As New User("max")
u.EmailAddresses.Add(New Email("max@hibernate.org"))
u.EmailAddresses.Add(New Email("max.andersen@jboss.com"))
u.EmailAddresses.TestMyList() ''TestMyList' is not a member of 'System.Collections.IList'.
End Sub


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 03, 2007 8:09 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
You will have to define these types:

- the collection interface (IMyList, for example), containing all methods you want to call on the collection. This is needed because NHibernate needs to be able to substitute a proxy for the real collection.

- the NHibernate-aware implementation (PersistentMyList, implementing IPersistentCollection, probably deriving from AbstractPersistentCollection or PersistentList, or whatever). This NHibernate-aware implementation will track changes made to it.

- the user collection type (MyListType : IUserCollectionType) which knows about your interface and the NHibernate-aware implementation.

You will almost certainly have your own implementation of the collection interface as well (MyList : IMyList), this implementation will not be aware of NHibernate, to allow your objects to be independent from NHibernate.

If you don't care about your objects being decoupled from NHibernate, you could probably reduce the number of classes implemented to just two (the NHibernate-aware collection and the IUserCollectionType). I think this should work but I have never tried.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 03, 2007 8:46 am 
Beginner
Beginner

Joined: Mon Mar 26, 2007 5:47 am
Posts: 22
I appreciate your help, but it doesn't seem to help me a lot further :-(

I do have the impression that I can't use my CustomList, I will always end up rewriting my CustomList in some new List inheriting form PersistingList-things. But that can impossible be the goal! I want to use my own CustomList inheriting from BindingList or whatever, bot rewrite all my things in my CustomList in some Persist-Thing-Stuff-List etc.


Could you please post a small bit of code that points me to the right direction? Because with telling me which files to write I don't get any closer: I should knwo what's inside the files :-/
Or point me to the guy that implemented the whole IUserCollectionType-stuff? He should know how it works I guess?
Or is their somewhere out there a working sample?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 03, 2007 8:55 am 
Beginner
Beginner

Joined: Mon Mar 26, 2007 5:47 am
Posts: 22
Actually, what would help me the most is simply the clue that would make the sample work...


What do I have to do to make the above sample (MyList.TestMyList) work?


Do I have to desribe TestMyList in an interface?

Or can I simply add some things to MyList to make it NHibernate-aware without having it to inherit from a specific class?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 6:40 pm 
Newbie

Joined: Fri Jan 12, 2007 2:28 pm
Posts: 10
Location: Houston, TX
Pieter - take a look at Damon Carr's implementation of IUserCollectionType here, it worked for me:

http://damon.agilefactor.com/2007/07/nhibernate-custom-collections.html

- David McClelland


Top
 Profile  
 
 Post subject: Are you guys serious!!!!!??????
PostPosted: Tue Feb 17, 2009 6:44 am 
Newbie

Joined: Tue Feb 17, 2009 6:37 am
Posts: 1
What the hell are you people thinking at Hibernate or N-Hibernate ...

Why are List's so rubbish in NHibernate? And why has nobody sorted this out? It is not hard ...

1. First problem, I have to use a rediculous interface IList for all my bags with little or no methods which support searching, sorting or predicates.

2. Secondly, I have to jump through 20 hoops to get my properties to use List<T>, seriously is that too much to ask?

Sometimes I feel that I end up banging my head against the wall for nothing either because of bad design, or crappy documentation. I am so over this shyte product.


Top
 Profile  
 
 Post subject: Re: Custom List: Unable to cast object of type ...
PostPosted: Wed Jun 02, 2010 3:34 am 
Newbie

Joined: Mon Apr 19, 2010 6:22 am
Posts: 2
First create a class that implements [IComparer] interface and define the [Compare] method.Following is the class that we will use for sorting Clients order by last name.
[VB.NET CODE STARTS]

Public Class ClientComparer
Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim objClientX As Client = CType(x, Client) ' Client is the class having FirtsName, LastName as properties
Dim objClientY As Client = CType(y, Client)

Dim sX As String = UCase(objClientX.LastName) ' comparision is made using the LAST NAME OF CLIENT
Dim sY As String = UCase(objClientY.LastName)

If sX = sY Then
Return 0
End If

If sX.Length > sY.Length Then
sX = sX.Substring(0, sY.Length)
If sX = sY Then
Return 1
End If
ElseIf sX.Length < sY.Length Then
sY = sY.Substring(0, sX.Length)
If sX = sY Then
Return -1
End If
End If

For i As Integer = 0 To sX.Length
If Not sX.Substring(i, 1) = sY.Substring(i, 1) Then
Return Asc(CType(sX.Substring(i, 1), Char)) - Asc(CType(sY.Substring(i, 1), Char))
End If
Next
End Function

End Class

[VB.NET CODE ENDS]

http://www.mindfiresolutions.com/Sorting-a-user-defined-collection-class-877.php


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

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.