我工作的地方,成员变量的约定是“m_(name)”,getter 和 setter 是“(get/set)Name”。
所以,我有:
事故名称.java
@XmlInverseReference(mappedBy = "m_occupants")
public AccidentVehicle getAccidentVehicleRecord() {
...
}
事故车辆.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(变量名称) 完全匹配?
最佳答案
我已经能够重现您所看到的问题。我在这里输入了一个错误:
解决方法
您可以切换为使用字段访问。
事故车辆
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;
}
}
事故名称
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;
}
}
关于java - Moxy:XmlInverse 和 getter/setter 与属性名称不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22058491/