我的应用程序在 Payara 上运行并提供 REST API,但是当我尝试使用 POST 请求创建对象时,出现以下异常:
Exception [EclipseLink-43] (Eclipse Persistence Services - 2.6.0.payara-p1): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Missing class for indicator field value [Miss] of type [class java.lang.String].
Descriptor: XMLDescriptor(at.htl.handballinheritance.entity.Throw --> [DatabaseTable(event), DatabaseTable(dataObject)])
at org.eclipse.persistence.exceptions.DescriptorException.missingClassForIndicatorFieldValue(DescriptorException.java:940)
at org.eclipse.persistence.internal.oxm.QNameInheritancePolicy.classFromRow(QNameInheritancePolicy.java:278)
...
相反,当我在使用 Hibernate 的 Wildfly 上执行程序时,一切都很好,没有发生错误。
实体:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@XmlRootElement
public abstract class Event extends DataObject implements Serializable {
...
}
@Entity
public class Throw extends Event implements Serializable {
@Enumerated(EnumType.STRING)
@NotNull
private ThrowingType type;
...
}
public enum ThrowingType {
Goal, Miss
}
REST API:
@POST
public Response create(Throw entity) {
em.persist(entity);
return Response.created(URI.create("...")).build();
}
我认为 Eclipselink 在解组我的 json 时遇到问题。我需要任何额外的注释吗?
如果您使用名为“type”的字段,您将收到此错误。
连接继承默认使用“type”字段来确定要使用的实体类,就像 json/xml 编组一样。您有一个类型字段,但用“Miss”填充它,无法转换为类。
请参阅 eclipselink/Moxy :基于类型的继承和属性名称重载了解自定义策略,或https://www.eclipse.org/eclipselink/documentation/2.4/moxy/type_level003.htm(如果您可以更改 JSON)已发布。
这里对 JPA 中的类型字段进行了一些讨论 http://www.coderanch.com/t/489497/ORM/databases/InheritanceType-JOINED-DiscriminatorColumn 和这里 https://en.wikibooks.org/wiki/Java_Persistence/Inheritance#No_class_discriminator_column_2
Eclipselink 找到了一条具有上述继承鉴别器值“Miss”的记录。您需要指定一个继承类,该类使用 @DiscriminatorValue(value = "Miss") 定义该类型。
如果有很多这样的未定义类型,那么继承是否是一个好的决定是值得怀疑的。您可以考虑将模型更改为聚合,在继承的类中聚合继承的实体,并将“类型”字段定义为继承的类中的普通列。
如果你根本不需要继承类型“Miss”,你可能会捕获 DescriptorException / PersistenceException。