Moxy:XmlInverse和getter / setter不同于属性名称

时间:2014-02-27 03:34:08

标签: java jaxb moxy

我在一个成员变量约定为“m_(name)”且getter和setter为“(get / set)Name”的地方工作。

所以,我有:

AccidentName.java

@XmlInverseReference(mappedBy = "m_occupants")
public AccidentVehicle getAccidentVehicleRecord() {
    ...
}

AccidentVehicle.java

@XmlElementWrapper( name="Occupants" )
@XmlElements( @XmlElement( name="AccidentName" ) )
public Set<AccidentName> getOccupants() {
    return m_occupants;
}

我正在尝试使用Moxy的注释来定义反向引用,但我收到的错误是这样的:

Exception [EclipseLink-59] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The instance variable [accidentVehicleRecord] is not defined in the domain class [net.denali.inpursuit.rms.data.persistent.accident.AccidentName], or it is not accessible.
Internal Exception: java.lang.NoSuchFieldException: accidentVehicleRecord
Mapping: org.eclipse.persistence.oxm.mappings.XMLInverseReferenceMapping[accidentVehicleRecord]
Descriptor: XMLDescriptor(net.denali.inpursuit.rms.data.persistent.accident.AccidentName --> [DatabaseTable(AccidentName), DatabaseTable(AuditableEntity)])

getter / setter是(get / set)AccidentVehicleRecord,但成员变量是m_accidentVehicleRecord。

我是否需要始终指定反向的两侧? 我是否需要让方法名称与模式get(变量名称)完全匹配?

1 个答案:

答案 0 :(得分:0)

我能够重现你所看到的问题。我在这里输入了一个错误:

解决方法

您可以切换为使用字段访问。

<强> AccidentVechicle

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class AccidentVehicle {

    @XmlElementWrapper( name="Occupants" )
    @XmlElements( @XmlElement( name="AccidentName" ) )
    private Set<AccidentName> m_occupants = new HashSet<AccidentName>();

    public Set<AccidentName> getOccupants() {
        return m_occupants;
    }

}

<强> AccidentName

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

@XmlAccessorType(XmlAccessType.FIELD)
public class AccidentName {

    @XmlInverseReference(mappedBy = "occupants")
    private AccidentVehicle m_accidentVehicleRecord;

    public AccidentVehicle getAccidentVehicleRecord() {
        return m_accidentVehicleRecord;
    }

}