The EJB 2.0 spec does not support the ORDER BY keyword. This is IMHO the biggest problem with CMP 2.0.
So you just can't do it!!
You have 2 choices : either do it in your business layer (my business layers spend 90% of their time ordering collections of value objects), either wait for EJB 2.1 which will have the ORDER BY statement (at least).
Any tips on sorting a Collection of found beans? Do you use a TreeSet and Comparator or did you hand code the sorting?
I'm using TreeMap in fact. There are some Open Source projects which provide faster implementations than the standard java.util.TreeMap, for example Jakarta Commons has an interesting FastTreeMap (see http://jakarta.apache.org/commons/collections.html ).
I'm not using a Set because :
1. My Value Objects are automatically generated by XDoclet, and I thought it was easier to do it this way than have them implement Comparable - which would have needed some XDoclet tweaking probably.
2. Sometimes I want to sort my VO in 2 or 3 different ways (by Date, by ID, by name, etc..).
Thanks for the comments. Just on the side, I did some quick tests this morning with "overloading" the query definition in jbosscmp-jdbc.xml to add an ORDER BY clause. It seems to work, something like this:
SELECT OBJECT(p) FROM User AS p WHERE p.deptId=?1 ORDER BY p.createDate
Of course, this doesn't strike me as a portable solution.
Hi, Nefilm and Julien:
Thank you very much.
Nefilim, could you tell me which jboss version that you used for your qiuick tests.
I tried on the JBoss-3.2.1. the Select ... clause was in the ejb-jar.xml, not in the jbosscmp-jdbc.xml. It can not be compiled after I added the ORDER BY in the ejb-jar.xml.
So I guess you may used other version.