While using Hibernate 3.1rc3 I found that my comparator was not being assigned for an internal SortedSet. This was causing a NullPointerException because my comparator was null and the internal objects in the set did not implement the Comparable interface.
This worked fine in Hibernate 2.1. I debugged the problem and found that some lazy initialization code for Comparator class creation in the org.hibernate.mapping.Collection.java
is as follows:
Code:
public Comparator getComparator() {
if(comparator!=null && comparatorClassName!=null) {
try {
setComparator( (Comparator) ReflectHelper.classForName(
comparatorClassName ).newInstance() );
}
catch (Exception e) {
throw new MappingException( "Could not instantiate comparator class: "
+ comparatorClassName + " for collection " + getRole() );
}
}
return comparator;
}
Note the line that checks to see if a new Comparator needs to be
instantiated.
[code]if(comparator!=null && comparatorClassName!=null) {...
If I am reading this correctly it means that the initialization code for the Compartor will only be excuted if the internal comparator reference is NOT null. Meaning that the initialization code is only called when we have already initialized a Comparator which means that it will never be initialized. The logic should probably be changed to read
[code]if(comparator ==null && comparatorClassName!=null) {...
Anyway I thougt I would post this here for someone to look at who is more familiar with the way in which Hibernate lazily instantiates Comparator classes for sorted collections.
[/code]