Search code examples
javahibernatejpainheritance

JPA Embeddable and inheritance with Hibernate


I've an entity inheriting from a class with attributes, let's say E1 inherits from C1. E1 is concrete, C1 may be abstract or concrete. E1 defines an embeddable attribute of type T1 and I need to map it in E2.

@Embeddable
public class T1 {
    ...
}

@MappedSuperclass
public abstract class C1 {
    private T1 t1;
}

@Entity
public class E1 extends C1 {
    // How to define the mapping of t1?
}

I can't figure out how to do it. As of my understanding, @AttributeOverrides needs to be defined close to the field, which is defined in C1. But the mapping needs to be defined in E1. Can someone help?

I searched over the internet without success and ended up by asking to ChatGPT. It suggests to redefine t1 in E1, which looks like a bad idea.


Solution

  • You can define @AttributeOverride for the child entity like this:

    @Entity
    @AttributeOverride(name = "t1.myField", column = @Column(name = "changed_column"))
    public class E1 extends C1 {