例外:“丢失类指示符字段从数据库行[UnmarshalRecordImpl()]”解组使用的Ecli

2019-08-17 23:04发布

这是任何方式与@ XmlDescriminatorNode / @ XmlDescrimintatorValue注释未来XML,或任何变通方法来解组:

<assets>
    <asset type="full">
        <data_file role="source">
            <locale name="ru-RU"/>
        </data_file>
        <data_file role="extension">
            <locale name="ru-RU"/>
        </data_file>
        <data_file>
            <locale name="ru-RU"/>
        </data_file>
    </asset>
</assets>

我的映射类:

@XmlRootElement(name="data_file")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlDiscriminatorNode("@role")
public abstract class BaseDataFile implements Serializable {

    @XmlPath("@role")
    @XmlAttribute(name = "role")
    private String role;

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }
}
@XmlRootElement(name="data_file")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlDiscriminatorValue("source")
public class SourceDataFile extends BaseDataFile {

}
@XmlRootElement(name="data_file")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlDiscriminatorValue("source")
public class SourceDataFile extends BaseDataFile {

}
@XmlRootElement(name="data_file")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlDiscriminatorValue("extension")
public class SourceDataFile extends BaseDataFile {

}
@XmlRootElement(name="asset")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlDiscriminatorNode("@type")
public abstract class BaseAsset implements Serializable {

    @XmlPath("@type")
    @XmlAttribute(name = "type")
    private String type;

    @XmlPath("data_file")
    private List<BaseDataFile> dataFiles;

    public List<BaseDataFile> getDataFiles() {
        return dataFiles;
    }

    public void setDataFiles(List<BaseDataFile> dataFiles) {
        this.dataFiles = dataFiles;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

错误occures如果XML containes元件如以下,没有“type”属性:

<data_file>
  <locale name="ru-RU"/>
</data_file>

提前致谢

Answer 1:

以下应该有所帮助:

Java模型

超级类( BaseDataFile

下面是你的一个简化版本BaseDataFile类。 既然你已经映射到XML属性role作为继承指标,你不需要它也映射到你的对象模型的属性。

import java.io.Serializable;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorNode;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({SourceDataFile.class, ExtensionDataFile.class})
@XmlDiscriminatorNode("@role")
public abstract class BaseDataFile implements Serializable {

}

如果你真的想在地图role XML属性的属性在你的对象模型,你应该使用莫西的@XmlReadOnly属性以防止它被编组到XML文档(这将已经wriiten为继承指标)。

import java.io.Serializable;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorNode;
import org.eclipse.persistence.oxm.annotations.XmlReadOnly;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({SourceDataFile.class, ExtensionDataFile.class})
@XmlDiscriminatorNode("@role")
public abstract class BaseDataFile implements Serializable {

    @XmlAttribute
    @XmlReadOnly
    String role;

}

子类( SourceDataFile

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorValue;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlDiscriminatorValue("source")
public class SourceDataFile extends BaseDataFile {

}

MISSING遗产指标

基类不是抽象

如果你的基类( BaseDataFile如果继承指标失踪的基类的一个实例)一直没抽象然后将已创建。

基类摘要

由于您的基类是抽象的莫西抱怨缺少继承指标值:

Exception in thread "main" Local Exception Stack: 
Exception [EclipseLink-44] (Eclipse Persistence Services - @VERSION@.@QUALIFIER@): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Missing class indicator field from database row [UnmarshalRecordImpl()].
Descriptor: XMLDescriptor(forum15597322.BaseDataFile --> [])
    at org.eclipse.persistence.exceptions.DescriptorException.missingClassIndicatorField(DescriptorException.java:957)
    at org.eclipse.persistence.internal.oxm.XMLRelationshipMappingNodeValue.processChild(XMLRelationshipMappingNodeValue.java:83)
    at org.eclipse.persistence.internal.oxm.XMLCompositeCollectionMappingNodeValue.startElement(XMLCompositeCollectionMappingNodeValue.java:184)
    at org.eclipse.persistence.internal.oxm.record.UnmarshalRecordImpl.startElement(UnmarshalRecordImpl.java:834)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:506)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:376)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2715)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:488)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
    at org.eclipse.persistence.internal.oxm.record.XMLReader.parse(XMLReader.java:221)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:895)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:388)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:366)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:323)
    at org.eclipse.persistence.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:367)
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:123)
    at forum15597322.Demo.main(Demo.java:23)

忽略错误

JAXB(JSR-222)实现包括编组/拆封过程中所遇到到MOXY报告异常ValidationEventHandler默认一个将错误输出遇到丢失继承指示符值时。 下面是设置自定义的一个例子ValidationEventHandler写着从未返回错误出truehandleEvent方法。

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    unmarshaller.setEventHandler(new ValidationEventHandler() {
        @Override
        public boolean handleEvent(ValidationEvent event) {
            return true;
        }

    });

而把这个答案在一起我发现下面的莫西错误,这为做上述的结果将使无效的文本值集合中的一个项目。 此修复程序是针对反对的EclipseLink 2.5.1。

  • http://bugs.eclipse.org/404269

一旦修复进去无效项只会被忽略。 这是你正在寻找的行为吗?


欲获得更多信息

  • http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-moxy-extension.html
  • http://blog.bdoughan.com/2010/12/jaxb-and-marshalunmarshal-schema.html
  • http://blog.bdoughan.com/2010/07/xpath-based-mapping.html


文章来源: Exception : “Missing class indicator field from database row [UnmarshalRecordImpl()].” when unmarshalling XML using EclipseLink JAXB (MOXy)