0

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 tried Deserialize JSON with Jackson into Polymorphic Types - A Complete Example is giving me a compile error

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.

Varun Sharma
  • 253
  • 1
  • 3
  • 16
  • 1
    Why don't you use a column in db to represent the type of product? As both type of products will be at the same table, just use a new column to define it. It will be simple to maintain in java model too. Read this article, it takes a lot of doubts about each mapping: https://www.baeldung.com/hibernate-inheritance – Dougllas Sousa May 21 '19 at 11:29
  • Firstly thanks for looking into this.. I thought about this but what if I have lot of different attributes in BuiltIn and CustomProducts? Then there will be lot of null valued columns for each type. – Varun Sharma May 21 '19 at 11:41
  • 1) the subclass does not extend Product 2) SINGLE_TABLE oder JOINED depends. If you have different mandatory attributes you should use JOINED. Maybe you could show use the entity code – Simon Martinelli May 22 '19 at 15:19
  • Sorry about 1. I forgot to add it while writing the question. I had extended Product. – Varun Sharma May 23 '19 at 10:45
  • But what is the problem? You don't need "extra info" for persistence as type column and entity class is enough to work both ways. You got problem with peristence or JSON/XML serialization? And where is that error? – Antoniossss May 23 '19 at 10:49

0 Answers0