我有以下类结构:
@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
答案 0 :(得分:1)
ListElement
是一个实体。所以你不能在@ElementCollection
中使用它,正如它的javadoc所示,
定义基本类型或可嵌入类
的实例集合
(强调我的)
您想要的是@OneToMany
,因为您在两个实体之间存在关联。
@NotFound
在toMany关联中没有任何意义。当你有一个由“外键”映射的toOne关联时,它会被使用,它不引用任何现有的行。