将 Comment 对象编码为 JSON 时出现以下异常:
javax.xml.bind.JAXBException: class javax.jdo.identity.LongIdentity nor any of its super class is known to this context. at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:594) at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:648)
下面是我的 Comment 定义,请注意我混合了 JAXB 注释(用于编码)和 JPA 注释(用于 GAE 持久化)。
@Entity
@XmlRootElement(name = "Comment")
@XmlAccessorType(XmlAccessType.FIELD)
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@XmlElement(name = "CommentId")
private Long commentId;
@Basic
@XmlElement(name = "Author")
private String author;
...
}
我不明白为什么异常与 LongIdentity 有关?
最佳答案
尝试注释属性而不是字段。 JPA 实现可能使用字节码操作来添加 LongIdentity 类型的字段。
关于google-app-engine - 使用 JPA 注释编码类时出现 JAXBException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4815358/