How to create multilevel inheritance strategy in hibernate

65 Views Asked by At

When i have only product class and two classes that extends product with inheritance strategy single table(product -> electronics , garden) it works properly.But if i try to create new single_table inheritance relationship (electronics -> smartphone,computer) i have a warn:

Discriminator column has to be defined in the root entity, it will be ignored in subclass: com.example.demo.entity.Electronics

Product class

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@DiscriminatorColumn(name = "type")
public abstract class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "info")
    private String info;

    @Column(name = "basic_characteristics")
    private String basicCharacteristics;

    @Column(name = "price")
    private Integer price = 0;
    }

Electronics Class:

@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
@DiscriminatorValue("electronics")
@DiscriminatorColumn(name = "typeElectronics")
public class Electronics extends Product {

    @Column(name = "electronics_data")
    private String electronicsData;
}

Smartphone class:

@AllArgsConstructor
@NoArgsConstructor
@Entity
@Data
@DiscriminatorValue("smartphone")
public class Smartphone extends Electronics{

    @Column
    private String smartphoneInfo;
}

I want to divide electronics class into two different entities with different fields.

1

There are 1 best solutions below

0
Crack_it On

https://docs.oracle.com/javaee/7/api/javax/persistence/DiscriminatorColumn.html

The strategy and the discriminator column are only specified in the root of an entity class hierarchy or subhierarchy in which a different inheritance strategy is applied

As per the documentation, I don't think Electronics class in this example can have additional discriminator column for its child classes.