Search code examples
javahibernatecomparisoncompare

Java/Hibernate comparator


Let's I have class

class Foo

and has two comparators for this class

class Comparator1 implements Comparator<Foo>  
class Comparator2 implements Comparator<Foo>  

now I am using TreeSet of Foo objects in my class Bar.

class Bar  
{  
   private SortedSet<Foo> foos = new TreeSet(new Comparator1());  
}  

in xml mapping

  <set name="foos"
     sort="com.package.Comparator2"
     fetch="subselect">
     <key column="column" not-null="true"/>
     <one-to-many class="Foo" />
  </set>  

Which comparator will be used by hibernate after loading set from database, and which will be used after persist?
Can I use in local modification Comparator1, but for loading and saving Comparator2?


Solution

  • Any collection will be rewritten while Hibernate loads entity from DB since it will need to use its own implementation of Set. If you created a brand new object and persisting it, then Comparator1 is still in use, because it's still your collection, it's not a Hibernate's proxy.