Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use the JPA @OrderBy annotation to sort in a case-insensitive way?

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.

like image 487
Dave Avatar asked Dec 06 '25 04:12

Dave


1 Answers

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());
like image 138
Craig Taylor Avatar answered Dec 07 '25 21:12

Craig Taylor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!