短い答え
boolean
戻り値の型は、ドキュメントエラーです。戻り値の型はvoid
である必要があります。
ロング回答私は意味
が、とにかく、 JAXBのStringにJPAのロング@Idを変換するため、この方法を使用するためには、それが持っていないとして、あなたはEclipseLink JAXB (MOXy)を使用することができ
を@XmlID @XmlID
で注釈を付けられたフィールド/プロパティがString
であるという制限。
JAXB-RIとMOXyなし。
あなたのユースケースをサポートマップするために
XmlAdapter
を使用することができ
:
IDAdapter
このXmlAdapter
は@XmlID
注釈の要件を満たすためにString
値にLong
値に変換します。@XmlJavaTypeAdapter
注釈がXmlAdapter
を指定するために使用され
package forum9629948;
import javax.xml.bind.DatatypeConverter;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class IDAdapter extends XmlAdapter<String, Long> {
@Override
public Long unmarshal(String string) throws Exception {
return DatatypeConverter.parseLong(string);
}
@Override
public String marshal(Long value) throws Exception {
return DatatypeConverter.printLong(value);
}
}
B
:
package forum9629948;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlAccessorType(XmlAccessType.FIELD)
public class B {
@XmlAttribute
@XmlID
@XmlJavaTypeAdapter(IDAdapter.class)
private Long id;
}
package forum9629948;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class A {
private B b;
private C c;
}
C
package forum9629948;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)public class C {
@XmlAttribute
@XmlIDREF
private B b;
}
デモ
package forum9629948;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(A.class);
File xml = new File("src/forum9629948/input.xml");
Unmarshaller unmarshaller = jc.createUnmarshaller();
A a = (A) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(a, System.out);
}
}
入力/出力
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<a>
<b id="123"/>
<c b="123"/>
</a>