JPA Enum Inheritance

I have an interface called ‘Category’ and 4 enum implementing this interface. Those enums are decided in categories and have different types in it. Example:

#Interface
interface BillCategory

#Enum 1
public enum GroceryPurchase implements BillCategory {
  VEGAN,  
  VEGETARIAN,
  PESCITARIAN,
  FLEXITARIAN, 
  OMNIVORE


#Enum 2
enum ElectronicPurchase implements BillCategory{ 
 SMARTPHONE, HARDWARE, SOFTWARE }

I aded the field to an entity class.

private BillCategory billCategory;

And when I start the Spring Environemnt I get the following Hibernate/JPA error:

Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: net.wizypay.wizypay.bill.model.BillCategory, at table: bill_entity, for columns: [org.hibernate.mapping.Column(bill_category)]

How can I solve this problem? I do not want to summarise the enums to one. Is there a better approach? Or just impossible to persist such data.

Answer

Alrght, not sure how Smartphone is NOT Hardware, but okay. Here is how I would do it (Not sure if I discard something you actually need now). Make BillCategory an enum. Add private fields PurchaseType (this could also be another enum containing values like GroceryPurchase or ElectronicPurchase) and Category (this is String and matches the enum name). That should solve your problem with hibernate.

Code illustrating what I mean:

enum BillCategory {

    VEGAN("VEGAN", PurchaseType.GroceryPurchase), 
    VEGETARIAN("VEGETARIAN",PurchaseType.GroceryPurchase),
    SMARTPHONE("SMARTPHONE", PurchaseType.ElectronicPurchase);

    private String category;
    private PurchaseType type;

    BillCategory(String category, PurchaseType type) {
        this.category = category;
        this.type = type;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public PurchaseType getType() {
        return type;
    }

    public void setType(PurchaseType type) {
        this.type = type;
    }

}

enum PurchaseType {
    ElectronicPurchase, GroceryPurchase;
}

You should then also annotate private BillCategory billCategory; in your entity class with something like @Enumerated(EnumType.ORDINAL), or some other EnumType, depending on how you want it to be saved in the database

Leave a Reply

Your email address will not be published. Required fields are marked *