• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorted and with unique elements

 
Farl Mann
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

In order to get a sorted and without repeated elements collection, I'm trying to do:

Collection coll = erh.findAll(); //erh is the RemoteHome
Collection coll2 = new TreeSet(coll);
sesion.setAttribute("coll2", coll2);
RequestDispatcher rd = request.getRequestDispatcher("/myjsp.jsp");
rd.forward(request, response);

But I get a java.lang.ClassCastException. The API says that
TreeSet(Collection c) throws ClassCastException - if the keys in the specified collection are not comparable, or are not mutually comparable.

It's been long hours trying so I'm too stuck to get what the API says, what I'm doing wrong .. and most of all, why the hell it doesn't work ... please, please, someone give me a hand

Many thanks in advance
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35704
407
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Farl,
findAll() returns a collection of entity beans. These don't implement Comparable and therefore you are getting the error.

I would recommended getting value objects (java beans) for each EJB before sorting. If you don't want to do that, you could write a Comparator to sort by a specific field in the EJB.
 
Prakash Dwivedi
Ranch Hand
Posts: 452
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Farl Mann:
[QB]

In order to get a sorted and without repeated elements collection, /QB]


why not have another finder method with "order by" clause. I could not understand what did you meant by "without repeated elements collection"? can you please explain it, as far as i know finder methods will return collection of the data found where one element represents one record in the database.
 
Farl Mann
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeanne, I get what you say about findAll() returning a collection of entity beans so I cannot use Comparable, but I'm quite a greenhorn to understand the rest of your answer .. how do I get value objects (java beans) for each EJB before sorting or how do I write a Comparator to sort by a specific field in the EJB??

Prakash, I'm going to try with ORDER BY, surely it will work for the sorting but, I still need my collection without repeated elements which means: imagine that my collection has names inside of it, for example: Jeanne, Farl, Farl, Prakash and Farl. What I want is to print in myjsp.jsp: Jeanne, Farl and Prakash, just printing the repeated elements (Farl) once � how is that done?? The Treeset was my only idea

Many thanks to both of you again
 
Jason Hunt
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
still need my collection without repeated elements


try use DISTINCT keyword in your EJB-QL.
 
Farl Mann
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been trying but it doesn't work..
These are some of my tries:

SELECT DISTINCT OBJECT(o)
FROM Concert AS o
ORDER BY o.date DESC

or

SELECT DISTINCT OBJECT(o)
FROM Concert AS o
ORDER BY o.code DESC

All of them end up with FindeExceptions.
My findAll method retrieves a collection and has no input parameters.. is this the bug?

What am I doing wrong???
Thanks again
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!