# Inheritance JOINED with two entity mapped in same table

**URL:** <https://discourse.hibernate.org/t/inheritance-joined-with-two-entity-mapped-in-same-table/5437>\
**Category:** Hibernate ORM\
**Created:** [June 16, 2021, 8:58am UTC](https://discourse.hibernate.org/t/inheritance-joined-with-two-entity-mapped-in-same-table/5437 "2021-06-16T08:58:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![boutss](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/boutss/32/1543_2.png) [@boutss](https://discourse.hibernate.org/u/boutss)\
**Post date:** [June 16, 2021, 8:58am UTC](https://discourse.hibernate.org/t/inheritance-joined-with-two-entity-mapped-in-same-table/5437/1 "2021-06-16T08:58:33Z")

</div>

Hello,

Here is the class model, namely that we can reference either ActorEcore or Actor which forces me to define them both as @Entity.  
The problem is that by doing this, as the two tables are persisted in the same table I have this error:

> org.hibernate.AnnotationException: Foreign key circularity dependency involving the following tables: TACTEUR, TACTEUR

Here he proposes to change the inheritance strategy to SINGLE\_TABLE and to use @SecondaryTable but it is not very simple because it forces to redefine all the attributes to declare the secondary table used

> <https://stackoverflow.com/questions/1825676/changing-the-inheritance-strategy-in-branches-of-the-class-hierarchy-via-jpa-ann>

![image](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/a/a674430f58d250d840d4ba62b75ba4219f07dbc3.png)

```auto
@Entity
@Table(name = "TACTEUR")
@Inheritance(strategy = JOINED)
@DiscriminatorColumn(name = "ROLE", length = 80)
public abstract class ActeurEcore

@Entity
public abstract class Acteur extends ActeurEcore

@MappedSuperclass
@Table(name = "TPERSONNEMORALE")
public abstract class PersonneMorale extends Acteur

@Entity
@Table(name = "TAGENCEBANCAIRE")
@DiscriminatorValue("com.hermes.ref.acteur.businessobject.AgenceBancaire")
@GenerationParameter(value = "com.hermes.ref.acteur.businessdao.ActeurDAO")
public class AgenceBancaire extends PersonneMorale

```

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [June 17, 2021, 7:08am UTC](https://discourse.hibernate.org/t/inheritance-joined-with-two-entity-mapped-in-same-table/5437/2 "2021-06-17T07:08:53Z")

</div>

You could try to map `Acteur` as `@MappedSuperclass` instead if you really need that class.

---

<div class="post-metadata">

**Author:** ![boutss](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/boutss/32/1543_2.png) [@boutss](https://discourse.hibernate.org/u/boutss)\
**Post date:** [June 17, 2021, 8:32am UTC](https://discourse.hibernate.org/t/inheritance-joined-with-two-entity-mapped-in-same-table/5437/3 "2021-06-17T08:32:24Z")

</div>

The problem if I convert to @MappedSuperClass I can no longer reference the Actor class.  
I had tried this already.  
The error: @entity Actor not found

I left with the solution of the SINGLE\_TABLE strategy using the @SecondaryTable annotation.  
It’s not more elegant but it seems to work.  
I have to redefine everywhere on all the attributes of the child classes the secondary table with @Column (table = “secondTable”)

---

<div class="post-metadata">

**Author:** ![Kilian\_S](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/kilian_s/32/1681_2.png) [@Kilian\_S](https://discourse.hibernate.org/u/Kilian_S)\
**Post date:** [October 19, 2021, 9:03am UTC](https://discourse.hibernate.org/t/inheritance-joined-with-two-entity-mapped-in-same-table/5437/4 "2021-10-19T09:03:10Z")

</div>

Thanks for your answer, it help me a lot 😉 !
