With the following entity classes:
@Entity
public class Account {
@Id
private int id;
@Basic(optional = false)
private String name;
}
@Entity
public class User {
@Id
private int id;
@ManyToMany
@OrderBy("name")
private Set<Account> accounts;
}
the following criteria
Criteria crit = session.createCriteria(User.class);
crit.addOrder(Order.asc("id"));
crit.list();
yields this SQL statement:
select ... from user_table this_ left outer join user_account accounts2_ on this_.id=accounts2_.user_id left outer join account account3_ on accounts2_.account_id=account3_.id order by account3_.name, this_.id asc
The columns in the order by clause are inversed. I would expect the result to be sorted by user id's.
The cause is JoinWalker.orderBy(List, String), which puts the order by columns from the associations first. Changing this to
return mergeOrderings( orderBy, orderBy( associations ) );
would probably fixed the problem.
If this change is desirable, I can add test and provide a GitHub pull request.
|