I’m using JPA 2.0, Hibernate 4.1.0.Final, and MySQL 5.5. I want to use the @OrderBy annotation to sort a set in one of my entities …
@OneToMany(mappedBy = "classroom", cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, orphanRemoval=true)
@OrderBy(“name”)
private Set<User> roster;
However, I would like the set to be sorted in a case-insensitive way, or at least sort everything according to the lower-case of the "name" attribute. Is this possible with JPA? I can upgrade my JPA and Hibernate versions if this solves my problem.
IF you want to stick with the standard JPA annotations and not go with a provider (Hibernate) specific ones it's not possible. However, by making use of a TreeSet with your own comparator you can achieve the same behavior.
eg:
Set<User> roster = new TreeSet<User>(new MyComparator());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With