-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Order by child entity fields
PostPosted: Mon Jul 18, 2011 11:50 am 
Newbie

Joined: Mon Jul 18, 2011 11:23 am
Posts: 4
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.