I have changed your code, to demonstrate the issue:
@Entity
public class Invoice {
@OneToMany(mappedBy = "invoice", fetch= FetchType.EAGER, orphanRemoval=true, cascade= CascadeType.ALL)
private List<InvoiceItem> itemList = new ArrayList<InvoiceItem>();
public Float getPrice() {
float price = 0f;
for(InvoiceItem ii : itemList){
price += ii.getPrice() * ii.getAmount();
}
return Float.valueOf(price);
}
public List<InvoiceItem> getItemList() {
return itemList;
}
@Override
public String toString() {
return itemList.toString();
}
}
@Entity
public class InvoiceItem{
private float price;
private float amount;
@ManyToOne
private Invoice invoice;
InvoiceItem(float price, float amount, Invoice i) {
this.price = price;
this.amount = amount;
this.invoice = i;
}
public float getPrice() {
return price;
}
public float getAmount() {
return amount;
}
public Invoice getInvoice() {
return invoice;
}
public void setInvoice(Invoice invoice) {
this.invoice = invoice;
}
@Override
public String toString() {
return amount + "*" + price;
}
}
public static void main(String[] args) {
com.objectdb.Enhancer.enhance("javaapplication.*");
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("$objectdb/db/test.odb");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Invoice i1 = new Invoice();
i1.getItemList().add(new InvoiceItem(1, 2, i1));
em.persist(i1);
Invoice i2 = new Invoice();
i2.getItemList().add(new InvoiceItem(2, 2, i2));
em.persist(i2);
Invoice i3 = new Invoice();
i3.getItemList().add(new InvoiceItem(5, 2, i3));
em.persist(i3);
Invoice i4 = new Invoice();
i4.getItemList().add(new InvoiceItem(4, 2, i4));
em.persist(i4);
Invoice i5 = new Invoice();
i5.getItemList().add(new InvoiceItem(3, 2, i5));
em.persist(i5);
em.getTransaction().commit();
Query query = em.createQuery(
"SELECT i FROM Invoice i ORDER BY i.getPrice() desc");
List resultList = query.getResultList();
System.out.println(resultList);
em.close();
emf.close();
}
The result list is everytime [[2.0*1.0], [2.0*2.0], [2.0*5.0], [2.0*4.0], [2.0*3.0]], so ther is no order...
Michael