Hi,
Environment:
JPA with hibernate3
MS SQL DB
I have the following entity hierarchy -
Customer
Orders [0...*]
OrderItems [0...*]
All relations are unidirectional.
I am using java.util.ArrayList for the entity collections with the below signature -
A) List of "Orders" in "Customer" entity -
Code:
@OnetoMany (cascade = "ALL", orphanRemoval = true, fetch = FetchType.LAZY)
@JoinColumn (name = "customer_id")
@IndexColumn (name = "indx", base = 0)
List <Orders> orders = new ArrayList<Orders>();
B) List of "OrderItems" in "Order" entity -
Code:
@OnetoMany (cascade = "ALL", orphanRemoval = true, fetch = FetchType.LAZY)
@JoinColumn (name = "order_id")
@IndexColumn (name = "indx", base = 0)
List <OrderItems> orderItems = new ArrayList<OrderItems>();
I am using below HQL to retrieve a particular customer along with all related child entities. I need the results ordered by the field "orderDesc" in "Orders" entity -
Code:
StringBuffer sb = new StringBuffer("
select DISTINCT Customer c
LEFT JOIN FETCH c.orders o
LEFT JOIN FETCH c.addresses a
LEFT JOIN FETCH o.orderItems oi
WHERE c.customerId = 1");
sb.append(" order by o.orderDesc DESC ");
List<Customer> custList = session.createQuery(sb.toString()).list();
If I execute the native query generated by hibernate in MSSQL query browser, the records are ordered correctly in DESC order by "Orders"."orderDesc" but the order of "Orders" entities in "custList " is per the insertion order.
I think its because of the "IndexColumn" defined in the entity since it works as expected if I use "java.util.Set" instead of "java.util.List". My entity classes are generated from XSD files using JAXB and I believe JAXB uses "java.util.List" as the default collection interface.
Is it possible to ensure the desired sorting order using "java.util.List" itself?
Regards,
Jacob