gpt4 book ai didi

java - Hibernate 和类继承。如何覆盖集合?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:12:44 25 4
gpt4 key购买 nike

我有以下类结构:

@MappedSuperclass
public abstract class MyAbstract implements Cloneable {

@ElementCollection(fetch = FetchType.EAGER)
@OrderColumn(name = "index_in_list")
@NotFound(action = NotFoundAction.IGNORE)
protected List<ListElement> list = null;
}

@Entity
@Table(name = "entity_1")
@AssociationOverrides({
@AssociationOverride(name = "list", joinColumns = @JoinColumn(name = "id_entity_1", nullable = false))
})
public class Entity1 extends MyAbstract {
}


@Entity
@Table(name = "entity_2")
@AssociationOverrides({
@AssociationOverride(name = "list", joinColumns = @JoinColumn(name = "id_entity_2", nullable = false))
})
public class Entity2 extends MyAbstract {
}

如您所见,我有两个具有相同字段(列表)的类。我想将类映射到它自己的表。这种关系是单向的。 ListElement 的表有两个字段:id_entity_1 和 id_entity_2。无论如何,我收到以下错误:

org.hibernate.AnnotationException: Illegal attempt to define a @JoinColumn with a mappedBy association: list

我决定在抽象类的字段上添加@JoinColumn 注解:

@ElementCollection(fetch = FetchType.EAGER)
@JoinColumn
@OrderColumn(name = "index_in_list")
@NotFound(action = NotFoundAction.IGNORE)
protected List<ListElement> list = null;

但是我得到了:

org.hibernate.MappingException: Duplicate property mapping of _listBackref found in ListElement

我曾经在 XML 文件中进行映射,但最近我决定改用注释。使用 XML 一切都很完美。 XML 结构是:

<class name="Entity1" table="entity_1">
<list name="list" cascade="all-delete-orphan">
<key column="id_entity_1" not-null="false"/>
<index column="index_in_list"/>
<one-to-many class="ListElement" not-found="ignore"/>
</list>
</class>

<class name="Entity2" table="entity_2">
<list name="list" cascade="all-delete-orphan">
<key column="id_entity_2"/>
<index column="index_in_list"/>
<one-to-many class="ListElement" not-found="ignore"/>
</list>
</class>

所以,我的问题是:如何通过注解来映射这种继承?

编辑 1:ListElement

定义的摘录是:

@Entity
@Table(name = "elements")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.INTEGER)
public abstract class AbstractElement implements Cloneable {
@Id
@Column(name = "id_element")
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected Long id = null;

@Column(name = "name", nullable = false, length = 255)
protected String name = null;

...and more simple properties
}

@Entity
@DiscriminatorValue("0")
public class ListElement extends AbstractElement {

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
@JoinColumn(name = "id_parent", nullable = false)
@MapKeyColumn(name = "key_in_map", nullable = false)
@OrderColumn(name = "id_xxx")
private Map<String, Xxx> xxx = null;

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
@JoinColumn(name = "id_parent", nullable = false)
@MapKeyColumn(name = "key_in_map", nullable = false)
@OrderColumn(name = "id_yyy")
private Map<String, Yyy> yyy = null;

...and methods
}

编辑 2:OneToMany

我用@OneToMany 替换了@ElementCollection:

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
@JoinColumn
@OrderColumn(name = "index_in_list")
protected List<ListElement> list = null;

我得到了:

org.hibernate.MappingException: Duplicate property mapping of _listBackref found in ListElement

最佳答案

ListElement 是一个实体。所以你不能在 @ElementCollection 中使用它,正如它的 javadoc 指出的那样,

Defines a collection of instances of a basic type or embeddable class

(强调我的)

你想要的是@OneToMany,因为你有两个实体之间的关联。

@NotFound 在 toMany 关联上也没有意义。当您有一个由不引用任何现有行的“外键”映射的 toOne 关联时使用它。

关于java - Hibernate 和类继承。如何覆盖集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11666820/

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