Version: Hibernate 3.0
I would appreciate any advice on the following code. This query returns a list with one element that is null if there are no payments for the specifed shipment. Notice that I need to check to see if the size is 1 and if the value is null and remove it so the for loop will not be entered. Is there a way to get the query to return an empty list if no payments are found?
Code:
List<Payment> payments = hsess.createQuery("select payment from Shipment as s left join s.payments as payment where s.id=:id order by payment.id")
.setInteger("id", shipment.getId())
.list();
if (payments.size() == 1 && payments.get(0) == null) payments.remove(0);
for (Payment payment : payments) {
// arbitrary payment manipulation
}