JPA and JAXB annotations in the same class cause com.sun.xml.bind.v2.runtime.IllegalAnnotationsException

I have 2 classes: Person and PersonAdapter. Person is generated from wsdl and cannot be changed.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Person")
public class Person {

    @XmlAttribute(name = "name")
    protected String name;

    // getters and setters
}

PersonAdapter is an object-adapter for Person and has some additional properties. Objects of this class are provided to clients of my service. I add all JPA and JAXB annotations to PersonAdapter class as I can't change Person class.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Person")
@Entity
@Table(name = "T_PERSON")
public class PersonAdapter {

    private final Person person;

    @XmlAttribute(name = "description")
    private String description;

    @XmlAttribute(name = "name", required = true)
    @Column(name = "C_NAME")
    public String getName() {
        return person.getName();
    }

    @Column(name = "C_DESCRIPTION")
    public String getDescription() {
        return description;
    }

    // getters, setters, contructors
}

I can't annotate name property because it's in Person class, so I annotate getName method with both JAXB and JPA annotations. But such usage of these annotations on the same method/property causes IllegalAnnotationsException:

Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 14 counts of IllegalAnnotationExceptions

Is it possible to solve this problem?

Upvotes: 2

Views: 230

Answers (1)

Solved! The problem wasn't in conflict between JPA and JAXB annotations. The problem was in clash of JAXB contexts: @XmlAccessorType(XmlAccessType.FIELD) on PersonAdapter with public getter of Person provides access of my service's JAXB context to Person object from other context. And Person class is unknown for my context. I solved this problem replacing XmlAccessType.FIELD with XmlAccessType.NONE. Also it can be done with @XmlTransient.

Upvotes: 1

Related Questions