• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorted and with unique elements

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 452
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic