'How to make inheritence with jpa and @MappedSuperclass with a field that is also a Parent with @MappedSuperclass

Here is the probleme :

@MappedSuperclass
@Data
class A {
    @Id
    private Long id;

    @ManyToOne
    protected B fieldB;
}

@MappedSuperclass
@Data
class B {
    @Id
    private Long id;
}

@Entity
@Table(name = "table_a1")
@AssociationOverride(name = "fieldB", joinColumns = @JoinColumn(name = "b1_id"))
public class A1 extends A {
    public A1() {
        this.fieldB = new B1();
    }
}

@Entity
@Table(name = "table_b1")
class B1 extends B {
}

@Entity
@Table(name = "table_a2")
@AssociationOverride(name = "fieldB", joinColumns = @JoinColumn(name = "b2_id"))
class A2 extends A {
    public A2() {
        this.fieldB = new B2();
    }
}

@Entity
@Table(name = "table_b2")
class B2 extends B {
}

I want to have one table per childs for each MappedSuperclass. The idea is to user theses SuperClass in a kind of 'generic' services. When i try the code above i have these error : '@OneToOne or @ManyToOne on com.example.demo.dao.entity.A1.fieldB references an unknown entity: com.example.demo.dao.entity.B'. Is there a way to 'redefine' the the right field B in each A subclass so that hibernate understand the correct mapping. Thanks for your participation !



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source