3

我有以下类结构:

@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:列表元素

定义的摘录是:

@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 替换了@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
4

1 回答 1

1

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

定义基本类型或可嵌入类的实例集合

(强调我的)

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

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

于 2012-07-26T10:28:23.990 回答