简体   繁体   中英

Hibernate and class inheritance. How to override collection?

I have the following class structure:

@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 {
}

As you can see I have two classes which have the same field (a list). I want to map the classes to its own tables. The relation is unidirectional. ListElement's table has both fields: id_entity_1 and id_entity_2. Anyway I got the following error:

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

I decided to add @JoinColumn annotation to the field in abstract class:

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

But I got:

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

I used to do mapping in XML file but recently I decided to use annotations instead. Using XML everything worked perfectly. The XML structure was:

<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>

So, my question is: How to map such inheritance by annotations?

EDIT 1: ListElement

An extract of the definition is:

@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
}

EDIT 2: OneToMany

I've replaced @ElementCollection by @OneToMany:

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

and I got:

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

ListElement is an entity. So you can't use it in a @ElementCollection which, as its javadoc indicates,

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

(emphasis mine)

What you want is @OneToMany , since you have an association between two entities.

The @NotFound doesn't make sense either on a toMany association. It's used when you have a toOne association mapped by a "foreign key" which doesn't reference any existing row.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM