gpt4 book ai didi

java - EclipseLink @MappedSuperclass 和泛型

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:23:24 25 4
gpt4 key购买 nike

我的 Web 应用程序中有几个领域模型类,它们之间存在层次关系。其中一个例子是用于对用户帖子进行分类的分层类别结构。

有一些与这些类的层次结构相关的逻辑是常见的。因此,我尝试将逻辑移动到通用的 @MappedSuperclass 注释父类(super class)中。

类似的东西:

@MappedSuperclass
public abstract class HierarchicalBaseEntity<N extends HierarchicalBaseEntity<N>>
extends BaseEntity {

@ManyToOne(optional=true)
@JoinColumn(name="parent")
private N parent;

private int depth;

public N getParent() { ...
public void setParent(N newParent) { ...

public boolean isRoot() { ...
public int getDepth() { ...

public boolean isDescendantOf(N ancestor) { ...
public static <N extends HierarchicalBaseEntity<N>> N getCommonAncestor(N a, N b) { ...
public static <N extends HierarchicalBaseEntity<N>> Collection<N> reduceToCommonAncestors(Collection<N> entities) { ...
}

子类然后扩展 HierarchicalBaseEntity 给自己作为通用类型 N:

@Entity
public class CategoryBean extends HierarchicalBaseEntity<CategoryBean> {

在 Java 中,这一切都非常干净。但不幸的是,EclipseLink 似乎不喜欢通用的“父”字段:

private N parent;

它给出了以下异常:

Caused by: Exception [EclipseLink-7250] (Eclipse Persistence Services - 2.1.0.v20100614-r7608): org.eclipse.persistence.exceptions.ValidationException
Exception Description: [class net.timp.yaase.core.model.HierarchicalBaseEntity] uses a non-entity [class java.lang.String] as target entity in the relationship attribute [field parent].
at org.eclipse.persistence.exceptions.ValidationException.nonEntityTargetInRelationship(ValidationException.java:1341)
at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.RelationshipAccessor.getReferenceDescriptor(RelationshipAccessor.java:416)
at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.ObjectAccessor.processOneToOneForeignKeyRelationship(ObjectAccessor.java:609)
at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.ObjectAccessor.processOwningMappingKeys(ObjectAccessor.java:678)
at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.ManyToOneAccessor.process(ManyToOneAccessor.java:107)

为什么它提示非实体字符串?

作为测试,我尝试删除泛型并仅将父字段定义为:

private HierarchicalBaseEntity parent;

没有泛型,EclipseLink 给出了这个异常:

Caused by: Exception [EclipseLink-7250] (Eclipse Persistence Services - 2.1.0.v20100614-r7608): org.eclipse.persistence.exceptions.ValidationException
Exception Description: [class net.timp.yaase.core.model.OnymBean] uses a non-entity [class net.timp.yaase.core.model.HierarchicalBaseEntity] as target entity in the relationship attribute [field parent].
at org.eclipse.persistence.exceptions.ValidationException.nonEntityTargetInRelationship(ValidationException.java:1341)
at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.RelationshipAccessor.getReferenceDescriptor(RelationshipAccessor.java:416)
at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.ObjectAccessor.processOneToOneForeignKeyRelationship(ObjectAccessor.java:609)
at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.ObjectAccessor.processOwningMappingKeys(ObjectAccessor.java:678)
at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.ManyToOneAccessor.process(ManyToOneAccessor.java:107)

True HierarchicalBaseEntity 在任何一种情况下都不是实体,是@MappedSuperclass..但是有没有办法用泛型或其他方式做到这一点?您的 @MappedSuperclass 中似乎没有字段引用它的子类之一。

最佳答案

问题是,当使用泛型作为关系的字段类型时,EclipseLink 直到运行时检查实际实例时才能知道目标类型是什么。因此映射必须在运行时动态创建,这是不支持的。

您可以继续使用通用父类(super class),但这需要将字段移动到实体,在那里它们将定义类型,然后为那些返回对象的字段具有抽象的内部 getter/setter,通用方法将调用转换为通用类型。令人费解,但它会允许通用 MappedSuperclass。

关于java - EclipseLink @MappedSuperclass 和泛型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3435760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com