I'm trying to marshall an arrangment of 3 entites taking advantage of MOxy's @XmlInverseReference annotation. My scenario very much mirrors the example provided here provided here
Entity A holds a collection of entity B which in turn holds a collection of entity C. The difference is that the collection of C in Entity B is sorted using a custom comparator. When I try to marshal the tree I get a ClassCastException where the collection in entity B is checked to see if contains entity A, causing the exception in the custom comparator which only accepts entity C
So here is the declaration of the collection of B in A
@XmlElement
private Set<AssignmentPeriod> periods = new LinkedHashSet<AssignmentPeriod>();
This is the collection of C in B which is sorted using the custom comparator
@XmlElement
@XmlInverseReference(mappedBy="assignmentPeriod")
@Sort(type = SortType.COMPARATOR,comparator=AssignmentPeriodRateComparator.class)
private Set<AssignmentPeriodRate> rates = new LinkedHashSet<AssignmentPeriodRate>();'
And finally the inverserse reference to entity B in C
@XmlElement
@XmlInverseReference(mappedBy="rates")
private AssignmentPeriod assignmentPeriod;
When I remove any reference to the comparator, it marshalls absolutely fine, when I add the comparator annotation back in I get a class cast exception where the List of AssignmentPeriodRate is checked to see if contains an object of type CandidateAssignment
The stack trace is below. Any pointers would be gratefully received!
Caused by: java.lang.ClassCastException: uk.co.rullion.mypayroll.entities.assignment.CandidateAssignment cannot be cast to uk.co.rullion.mypayroll.entities.assignment.AssignmentPeriodRate
at uk.co.rullion.mypayroll.entities.assignment.AssignmentPeriodRateComparator.compare(AssignmentPeriodRateComparator.java:1) [myPayrollEntities-0.0.1-SNAPSHOT.jar:]
at java.util.TreeMap.getEntryUsingComparator(Unknown Source) [rt.jar:1.7.0_17]
at java.util.TreeMap.getEntry(Unknown Source) [rt.jar:1.7.0_17]
at java.util.TreeMap.containsKey(Unknown Source) [rt.jar:1.7.0_17]
at java.util.TreeSet.contains(Unknown Source) [rt.jar:1.7.0_17]
at org.hibernate.collection.internal.PersistentSet.contains(PersistentSet.java:171) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at org.eclipse.persistence.internal.queries.CollectionContainerPolicy.contains(CollectionContainerPolicy.java:113) [eclipselink-2.5.1-RC1.jar:2.5.1.v20130824-981335c]
at org.eclipse.persistence.internal.queries.ContainerPolicy.contains(ContainerPolicy.java:701) [eclipselink-2.5.1-RC1.jar:2.5.1.v20130824-981335c]
at org.eclipse.persistence.internal.queries.ContainerPolicy.contains(ContainerPolicy.java:1) [eclipselink-2.5.1-RC1.jar:2.5.1.v20130824-981335c]
at org.eclipse.persistence.internal.oxm.XMLCompositeCollectionMappingNodeValue.marshal(XMLCompositeCollectionMappingNodeValue.java:97) [eclipselink-2.5.1-RC1.jar:2.5.1.v20130824-981335c]
at org.eclipse.persistence.internal.oxm.NodeValue.marshal(NodeValue.java:149) [eclipselink-2.5.1-RC1.jar:2.5.1.v20130824-981335c]
at org.eclipse.persistence.internal.oxm.NodeValue.marshal(NodeValue.java:102) [eclipselink-2.5.1-RC1.jar:2.5.1.v20130824-981335c]
at org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext.marshal(ObjectMarshalContext.java:59) [eclipselink-2.5.1-RC1.jar:2.5.1.v20130824-981335c]
at org.eclipse.persistence.internal.oxm.XPathNode.marshal(XPathNode.java:401) [eclipselink-2.5.1-RC1.jar:2.5.1.v20130824-981335c]
at org.eclipse.persistence.internal.oxm.XPathObjectBuilder.buildRow(XPathObjectBuilder.java:240) [eclipselink-2.5.1-RC1.jar:2.5.1.v20130824-981335c]
at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:118) [eclipselink-2.5.1-RC1.jar:2.5.1.v20130824-981335c]
at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:1) [eclipselink-2.5.1-RC1.jar:2.5.1.v20130824-981335c]
at org.eclipse.persistence.internal.oxm.XMLCompositeCollectionMappingNodeValue.marshalSingleValue(XMLCompositeCollectionMappingNodeValue.java:324) [eclipselink-2.5.1-RC1.jar:2.5.1.v20130824-981335c]
at org.eclipse.persistence.internal.oxm.XMLCompositeCollectionMappingNodeValue.marshal(XMLCompositeCollectionMappingNodeValue.java:104) [eclipselink-2.5.1-RC1.jar:2.5.1.v20130824-981335c]
at org.eclipse.persistence.internal.oxm.NodeValue.marshal(NodeValue.java:149) [eclipselink-2.5.1-RC1.jar:2.5.1.v20130824-981335c]
at org.eclipse.persistence.internal.oxm.NodeValue.marshal(NodeValue.java:102) [eclipselink-2.5.1-RC1.jar:2.5.1.v20130824-981335c]
at org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext.marshal(ObjectMarshalContext.java:59) [eclipselink-2.5.1-RC1.jar:2.5.1.v20130824-981335c]
at org.eclipse.persistence.internal.oxm.XPathNode.marshal(XPathNode.java:401) [eclipselink-2.5.1-RC1.jar:2.5.1.v20130824-981335c]
at org.eclipse.persistence.internal.oxm.XPathObjectBuilder.buildRow(XPathObjectBuilder.java:240) [eclipselink-2.5.1-RC1.jar:2.5.1.v20130824-981335c]
at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:118) [eclipselink-2.5.1-RC1.jar:2.5.1.v20130824-981335c]
at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:1) [eclipselink-2.5.1-RC1.jar:2.5.1.v20130824-981335c]
at org.eclipse.persistence.internal.oxm.XMLMarshaller.marshal(XMLMarshaller.java:751) [eclipselink-2.5.1-RC1.jar:2.5.1.v20130824-981335c]
at org.eclipse.persistence.internal.oxm.XMLMarshaller.marshalStreamOrWriter(XMLMarshaller.java:1128) [eclipselink-2.5.1-RC1.jar:2.5.1.v20130824-981335c]
at org.eclipse.persistence.internal.oxm.XMLMarshaller.marshal(XMLMarshaller.java:1079) [eclipselink-2.5.1-RC1.jar:2.5.1.v20130824-981335c]
at org.eclipse.persistence.internal.oxm.XMLMarshaller.marshal(XMLMarshaller.java:1034) [eclipselink-2.5.1-RC1.jar:2.5.1.v20130824-981335c]
at org.eclipse.persistence.jaxb.JAXBMarshaller.marshal(JAXBMarshaller.java:684) [eclipselink-2.5.1-RC1.jar:2.5.1.v20130824-981335c]
... 35 more