For example, there is a many-to-many association between A and B. And
A.bs is sorted List<B> that sorted by an index-column in the middle-table: idx
So we haven’t mapping idx column as Class field.
Now we want to query the list of B individually and the main condition is A’s id. But when query B directly, it comes out with an unsorted List (don’t order by the idx field).
I know we can retrieve A at first then collect the B into a List. However, considering the framework, we can do like that, it have to return a List without manual coding (don’t want to describe too much about the framework. in one word, this limitation is caused by the pagination tool encapsulate by our self, I don’t want to modify that.).
Is there any methods to solve this problem via HQL/Criteria. Criteria are preferable.
Okay. This problem has been solved. Using the index() function in hql will get the right result. such as:
The limitation of index() is that the children must be navigated by parent in hql or it would throw out an exception. means that hql like “select b from B b order by index(b)” is illegal.
Additionally, don’t use “fetch” in this association navigation(it’s not something concerned in this question).
hope it’s useful to someone who is puzzled on this.