Home > OS >  Hibernate override "mappedBy" property in embedded class
Hibernate override "mappedBy" property in embedded class

Time:11-07

Please tell me if can I override "mappedBy" property.

In my example there are two entities that are with relation to other one and have common part. For the sake of simplicity, I will omit fields like ids and functions (getters and setters).

Two parents:

@Entity
public class Parent1 {

    @Embedded
    //I want to use sth like: AssociationOverride on "other" to mappedBy="parent1"
    private Common common;
}


@Entity
public class Parent2 {

    @Embedded
    //I want to use sth like: AssociationOverride on "other" to mappedBy="parent2"
    private Common common;
}

With @ManyToOne relation:

@Entity
public class OtherEntity {

    
    @ManyToOne
    @JoinColumn(name="p_id_1")
    private Parent1 parent1;


    @ManyToOne
    @JoinColumn(name="p_id_2")
    private Parent2 parent2;
}

And the common part embedded in parent:

@Embeddable
public class Common {

    @OneToMany(mappedBy="IT_DEPENDS") //???
    private OtherEntity other;

}

As far as I understand @AssociationOverride works only for join column or tables. Is there any way to override mappedBy attribute?

CodePudding user response:

As it's stated in jpa specification (see section 2.7):

An embeddable class (including an embeddable class within another embeddable class) that is contained within an element collection must not contain an element collection, nor may it contain a relationship to an entity other than a many-to-one or one-to-one relationship. The embeddable class must be on the owning side of such a relationship and the relationship must be mapped by a foreign key mapping.

So, you can not use mappedBy side of one-to-many relationship in embeddable class.

  • Related