【问题标题】:JPA and JAXB annotations in the same class cause com.sun.xml.bind.v2.runtime.IllegalAnnotationsException同一类中的 JPA 和 JAXB 注释导致 com.sun.xml.bind.v2.runtime.IllegalAnnotationsException
【发布时间】:2016-05-05 14:57:13
【问题描述】:

我有 2 个类:Person 和 PersonAdapter。 Person 是从 wsdl 生成的,不能更改。

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

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

    // getters and setters
}

PersonAdapter 是 Person 的对象适配器,并具有一些附加属性。此类的对象提供给我的服务的客户。我将所有 JPA 和 JAXB 注释添加到 PersonAdapter 类,因为我无法更改 Person 类。

@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
}

我不能注释 name 属性,因为它在 Person 类中,所以我用 JAXB 和 JPA 注释来注释 getName 方法。但是在同一方法/属性上使用这些注释会导致 IllegalAnnotationsException:

原因:com.sun.xml.bind.v2.runtime.IllegalAnnotationsException:14 次 IllegalAnnotationExceptions

这个问题能解决吗?

【问题讨论】:

    标签: jpa jaxb annotations


    【解决方案1】:

    解决了!问题不在于 JPA 和 JAXB 注释之间的冲突。问题在于 JAXB 上下文的冲突:PersonAdapter 上的 @XmlAccessorType(XmlAccessType.FIELD)Person 的公共 getter 提供了从其他上下文访问我的服务的 JAXB 上下文到 Person 对象的权限。 Person 类在我的上下文中是未知的。我用XmlAccessType.NONE 替换XmlAccessType.FIELD 解决了这个问题。也可以通过@XmlTransient 完成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-28
      • 1970-01-01
      相关资源
      最近更新 更多