JPA Long @Id as JAXB @XmlID generates an XSD validation error

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.

+5
2

.

, @XmlID ?

, @XmlID @XmlIDREF JAXB.

JPA JAXB.

simple JPA @Id . JPA.

@XmlRootElement
public class Parent {

    @Id
    @XmlAttribute
    private Long id;

    @OneToMany
    @XmlElement(name = "child")
    @XmlElementWrapper
    private Collection<Child> children;
}


@XmlRootElement
public class Child {

    @XmlAttribute
    private Long getParentId() {
        return parent.getId();
    }

    @Id
    @XmlAttribute
    private Long id;

    @ManyToOne
    @XmlTransient // for preventing infinite circular problem
    private Parent parent;
}
+2

. EclipseLink JAXB (MOXy) JAXB (JSR-222).

@XmlSchemaType @XmlID @XmlIDREF. , JAXB RI , EclipseLink MOXy @XmlID. MOXy, , :

package forum11791735;

import java.util.List;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {

    @XmlID
    @XmlJavaTypeAdapter(JAXBLongAdapter.class)
    @XmlSchemaType(name="long")
    private Long id;

    @XmlIDREF
    @XmlSchemaType(name="long")
    private Employee manager;

    @XmlElement(name="report")
    private List<Employee> reports;

}

WORK AROUND

, , - . ValidationEventHandler Unmarshaller, ?

@XmlID/@XmlIDREF , MOXy @XmlInverseReference:

+2

All Articles