我正在尝试利用 MOxy 的 @XmlInverseReference 注释来编组 3 个实体。我的场景与此处提供的示例非常相似
实体 A 持有实体 B 的集合,而 B 又持有实体 C 的集合。不同之处在于,实体 B 中的 C 集合使用自定义比较器进行排序。当我尝试编组树时,我得到一个 ClassCastException,其中检查实体 B 中的集合以查看是否包含实体 A,从而导致仅接受实体 C 的自定义比较器中的异常
所以这里是A中B集合的声明
@XmlElement
private Set<AssignmentPeriod> periods = new LinkedHashSet<AssignmentPeriod>();
这是使用自定义比较器排序的 B 中 C 的集合
@XmlElement
@XmlInverseReference(mappedBy="assignmentPeriod")
@Sort(type = SortType.COMPARATOR,comparator=AssignmentPeriodRateComparator.class)
private Set<AssignmentPeriodRate> rates = new LinkedHashSet<AssignmentPeriodRate>();'
最后是 C 中实体 B 的反向引用
@XmlElement
@XmlInverseReference(mappedBy="rates")
private AssignmentPeriod assignmentPeriod;
当我删除对比较器的任何引用时,它编组绝对没问题,当我将比较器注释添加回来时,我得到一个类转换异常,其中检查 AssignmentPeriodRate 列表以查看是否包含 CandidateAssignment 类型的对象
堆栈跟踪如下。任何指点将不胜感激!
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