ngắn trả lời
Các boolean
kiểu trả về là một lỗi tài liệu. Loại trả về phải là void
.
dài trả lời
Ý tôi là, dù sao, sử dụng phương pháp này để chuyển đổi dài @ id JPA để JAXB của Chuỗi @XmlID
Bạn có thể sử dụng EclipseLink JAXB (MOXy) vì nó không có hạn chế rằng một trường/thuộc tính được chú thích với @XmlID
là loại String
.
với JAXB-RI và không có MOXy.
Bạn có thể sử dụng một XmlAdapter
để lập bản đồ hỗ trợ trường hợp sử dụng của bạn:
IDAdapter
XmlAdapter
này chuyển giá trị Long
đến một giá trị String
để đáp ứng các yêu cầu của @XmlID
chú thích.
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
Các @XmlJavaTypeAdapter
chú thích được sử dụng để xác định XmlAdapter
:
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;
}
Một
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;
}
Demo
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);
}
}
Input/Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<a>
<b id="123"/>
<c b="123"/>
</a>
Nguồn
2012-03-09 11:12:46