I implemented the REST API based on JPA and JAXB.
I have classes something like this (very simplified):
@Entity
@XmlRootElement
...
public class Thing {
@Id
@GeneratedValue
...
@XmlAttribute
@XmlID
@XmlJavaTypeAdapter(JAXBLongAdapter.class)
private Long id;
...
}
Hibernate (my current JPA provider) generates numbers as an id value, but they are naturally unique to just one type, Thing in this example.
XSD now says that xsd: id (@XmlID) is an NCString, which cannot be a prime, so I added '_' to the numbers in the JAXBLongAdapter. - like '_1'
Now the schema validator complains:
[org.xml.sax.SAXParseException: cvc-id.2: There are multiple occurrences of ID value '_1'.]
If I understand this correctly, the xsd: ID element must have a value (string), globally unique in the XML document. But this is very opposite to the general way of using identifiers in databases.
What should I do now? I thought of three things:
- Create a JAXBLongAdapter for each type with a type prefix?
- JPA, , UUID? - ?
- @XmlID @XmlIDREF, .
, .
- , , URL-.
: , ?
?
EDIT:
, JPA .
@XmlID
@XmlAttribute(name="id")
private String getXmlID(){
return String.format("%s-%s", this.getClass().getSimpleName(), this.getId().toString());
}
private void setXmlID(String xmlid){
String prefix = String.format("%s-", this.getClass().getSimpleName());
if(xmlid.startsWith(prefix)){
this.id = Long.parseLong(xmlid.substring(prefix.length()));
}else{
throw new IllegalArgumentException(xmlid+" does not look like "+prefix+"###");
}
}
JAXB Annotation / XmlID.