I am trying to create a table using Spring boot and Hibernate JPA. Now I have a situation where I have an Entity Product which can be of two types. BuiltInProduct or CustomProduct
Product is the super class and both BuiltInProduct and CustomProduct extend it.
Now I have another entity OrderItem which has a @ManyToOne
relationship with product.
So this OrderItem can have the product either as BuiltIn OR Custom.
I tried using @Inheritance
on Product with a @DiscriminatorColumn
as is_a
and extend this class to make BuiltInProduct and CustomProduct. My classes looks like
@Table(name = "products")
@Entity
@Inheritance
@DiscriminatorColumn(name = "is_a")
public abstract class Product {
// Getter Setters
}
The other two classes were like
@Entity
public class BuiltInProduct extends Product {
// Getter setters
}
Now since my Entity OrderItem can have either BuiltIn or Custom product, it had a field Product
public class OrderItem {
...
@ManyToOne
private Product product;
}
I know that the product is abstract class and hence I needed more info to serialize and deserialize into types.
I am still unable to get the desired output.
I am new to Spring and dont know how to handle these kind of scenarios.
Also I am not sure that I am using the correct @Inheritance
or should I use JOINED,TABLE_PER_CLASS approach.
I believe that JOINED will be more suited as ButilIn and CustomProducts will have different attributes but at the same time also am unable to figure how to make that work.
My use case is that I have a field that can have BuiltInProducts which can be selected or the user can add a custom product.