I have a @ManyToMany entity where I want to apply a numerical order to a String column. The most basic idea would be:
Code:
@OrderBy(value="code")
private List<Centre> centres = new ArrayList<Centre>();
But this doesn't work because codes 1,2,10 are ordered like 1,10,2.
I then thought I'd use the @Sort annotation. First attempt was:
Code:
@Sort(type = SortType.COMPARATOR,comparator=StringNumberComparator.class)
private List<Centre> centres = new ArrayList<Centre>();
But this doesn't seem to trigger the comparator and the items in the list are in insertion order 1,10,2.
I read in the documentation one needed to use a SortedSet, so I changed the code to:
Code:
@Sort(type = SortType.COMPARATOR,comparator=StringNumberComparator.class)
private SortedSet<Centre> centres = new TreeSet<Centre>();
Unfortunately, changing to this will give me new error elsewhere. In the code:
Code:
public void addTrial(ClinicalTrial trial) {
if(trial == null){
return;
}
getClinicalTrials().add(trial);
trial.getCentres().add(this);
}
I'll get the Exception:
Quote:
java.lang.ClassCastException: com.itclinical.iwrs.persist.model.CentreImpl cannot be cast to java.lang.Comparable
This is only solved if CentreImpl implements Comparable, but that makes the use of SortType.COMPARATOR useless.
Does anyone know how can we use the Comparator without having to implement Comparable on the underlying class?