It seems I have to take out the order by and do a sort in Java. From the code belowl the results are not sorted anymore (as I expected). Is sorting in Java the recommended approach or is there a better way?
Code:
Query query = session.createQuery(
"select shipment " +
"from Shipment shipment " +
" inner join shipment.cargoControlNumber.carrierCode.carrier.persons person " +
" inner join shipment.currentStatus currentStatus " +
" inner join currentStatus.releaseCode releaseCode " +
" left join fetch shipment.currentStatus " +
" left join fetch shipment.statusMessages " +
"where " +
" person.id = :personId and " +
" shipment.isPurged = false and " +
" releaseCode.number = :releaseCodeNumber and " +
" currentStatus is not null and " +
" currentStatus.date >= current_date - 7 " +
"order by currentStatus.date desc"
);
query.setParameter( "personId", personId );
query.setParameter( "releaseCodeNumber", releaseCodeNumber );
query.setFirstResult( firstResult );
query.setMaxResults( maxResults );
List list = query.list();
Set distinctShipments = new HashSet( list );
return Arrays.asList( distinctShipments.toArray() );