Search code examples
javahibernatejpa

Multiple abstract classes in JPA with the InheritanceType.SINGLE_TABLE


I have:

@Entity(name="products")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="product_type", 
  discriminatorType = DiscriminatorType.INTEGER)
public abstract class Product {
    // ...
}

And

@Entity
@DiscriminatorValue("1")
public class Book extends Product {
    // ...
}
@Entity
@DiscriminatorValue("2")
public class EBook extends Product {
    // ...
}

Now I want to add another abstract @Entity to prevent duplication of the general columns and methods related only to digital products:

@Entity
public abstract class DigitalProduct extends Product {
    // ...
}

And change parent of the EBook to it:

@Entity
@DiscriminatorValue("2")
public class EBook extends DigitalProduct {
    // ...
}

Do I need to mark the DigitalProduct with the @MappedSuperclass or single @Entity will be enough?


Solution

  • As it turned out, yes, it was enough to mark the DigitalProduct only with the annotation @Entity.