Anshu
Anshu

Reputation: 295

Auditing a SortedSet property using Javers gives java.lang.ClassCastException

I'm trying to audit a SortedSet property using Javers but I keep getting the below exception when I try to do a commit.

java.lang.ClassCastException: org.javers.core.metamodel.object.InstanceId cannot be cast to java.lang.Comparable
at java.util.TreeMap.compare(TreeMap.java:1188)
at java.util.TreeMap.put(TreeMap.java:531)
at java.util.TreeSet.add(TreeSet.java:255)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
at com.google.gson.Gson.fromJson(Gson.java:803)

This doesn't happen when I try to audit a property of type Set instead of SortedSet. (Also, Animal class implements Comparable and method compareTo()) Any help would be appreciated. Thanks.

Upvotes: 0

Views: 144

Answers (1)

dave
dave

Reputation: 11975

A basic (ie. unsorted) Set simply holds elements. A SortedSet applies an ordering to those elements and you must supply the ordering in some way. For example, by ensuring the elements implement the Comparable interface (for natural ordering) or by supplying a Comparator.

In your case, it looks like the sorting comparison requires comparing InstanceIds. (Either because you are adding them directly to the set, or the elements you are adding have InstanceIds.) As InstanceId does not implement Comparable, you get an error.

(If you supply a bit more of your code, eg. set creation and adding to it, it may be clearer what the cause of your error is.)

Upvotes: 0

Related Questions