MOXy의 NullPolicy를 사용하여이 동작을 지정할 수 있습니다. 기본 매핑을 수정하려면 DescriptorCustomizer를 만들어야합니다. 그것은 생각보다 쉽게 걱정하지 마세요, 나는 아래에 설명합니다 : 당신이 당신의 모델 클래스와 카스타을 연결하는 방법을
다음
import org.eclipse.persistence.config.DescriptorCustomizer;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping;
import org.eclipse.persistence.oxm.mappings.nullpolicy.XMLNullRepresentationType;
public class RootEntityCustomizer implements DescriptorCustomizer {
@Override
public void customize(ClassDescriptor descriptor) throws Exception {
XMLCompositeObjectMapping entityTwoMapping = (XMLCompositeObjectMapping) descriptor.getMappingForAttributeName("entityTwo");
entityTwoMapping.getNullPolicy().setNullRepresentedByEmptyNode(true);
entityTwoMapping.getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.EMPTY_NODE);
}
}
은 다음과 같습니다
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlCustomizer;
@XmlRootElement(name="RootEntity")
@XmlCustomizer(RootEntityCustomizer.class)
public class RootEntity {
private String attributeOne;
private Entity entityOne;
private Entity entityTwo;
@XmlElement(name="AttributeOne")
public String getAttributeOne() {
return attributeOne;
}
public void setAttributeOne(String attributeOne) {
this.attributeOne = attributeOne;
}
@XmlElement(name="EntityOne")
public Entity getEntityOne() {
return entityOne;
}
public void setEntityOne(Entity entityOne) {
this.entityOne = entityOne;
}
@XmlElement(name="EntityTwo")
public Entity getEntityTwo() {
return entityTwo;
}
public void setEntityTwo(Entity entityTwo) {
this.entityTwo = entityTwo;
}
}
을 MOXY의 다음 버전 (2.2) 주석을 통해이 작업을 수행 할 수 있습니다.
당신은 EclipseLink가 2.2.0 중 하나와 지금을 시도 할 수 있습니다
야간
@XmlElement(name="EntityTwo")
@XmlNullPolicy(emptyNodeRepresentsNull=true,
nullRepresentationForXml=XmlMarshalNullRepresentation.EMPTY_NODE)
public Entity getEntityTwo() {
return entityTwo;
}
를 구축 :
, 그래서 내 클래스에 주석을 추가 . 불행히도 비어 있지만 속성이 정의되어 있으면 요소를 null로 설정하는 동작이 있습니다. 예를 들어 위의 예제에서 EntityOne은 이제 null 객체를 반환합니다. 이것은 예상 된 행동입니까? – sdoca
이것은 버그입니다. https://bugs.eclipse.org/322183을 참조하십시오. 이번 주에 문제가 해결 될 것입니다. 올바른 동작을 보려면 속성 대신 요소의 id 속성을 만들 수 있습니다. –
속성에서 속성으로 변경되었으며 조금 더 자세한 XML을 의미하지만 작동합니다. 감사! – sdoca