Im creating a repair service with different types of repair. I have a SINGLE_TABLE inheritance and I differentiate them with an Enum repair_Type, I also want to have Repair_Type as a column so I can sort and search repairs by type but I am getting No enum constant
exception.
Here's my code:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING)
public abstract class Repair {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public long repairID;
@ManyToOne
private Customer customerID;
@Enumerated(EnumType.STRING)
private RepairType repairType;
@OneToMany
private List<Employee> employeeList;
@Enumerated(EnumType.STRING)
private DeliveryOption deliveryOption;
private boolean expressRepair;
private boolean cleanProduct;
@Column(length = 64)
private String productImage;
@OneToOne
private Brand brand;
private String model;
private int size;
private int buildYear;
@Temporal(TemporalType.TIMESTAMP)
private Date dateCreated;
@Column(nullable = false)
private String repairNote;
}
No getters and setters for brevity
RepairType:
public enum RepairType {
KITE(Types.KITE),
BOARD(Types.BOARD),
BAR(Types.BAR),
WETSUIT(Types.WETSUIT);
private String type;
RepairType(String type) {
}
public static class Types {
public static final String KITE = "Kite";
public static final String BOARD = "Board";
public static final String BAR = "Bar";
public static final String WETSUIT = "Wetsuit";
}
}
Example of a child class repair type (I have four)
@Entity
@DiscriminatorValue(value = RepairType.Types.BAR)
public class BarRepair extends Repair {
private boolean lengthCheck;
public BarRepair() {
}
public BarRepair(long repairID, Customer customerID, List<Employee> employeeList, DeliveryOption deliveryOption, boolean expressRepair, boolean cleanProduct, String productImage, Brand brand, String model, int size, int buildYear, Date dateCreated, String repairNote, boolean lengthCheck) {
super(repairID, customerID, employeeList, deliveryOption, expressRepair, cleanProduct, productImage, brand, model, size, buildYear, dateCreated, repairNote);
this.lengthCheck = lengthCheck;
}
public boolean isLengthCheck() {
return lengthCheck;
}
public void setLengthCheck(boolean lengthCheck) {
this.lengthCheck = lengthCheck;
}
}
Enum mapped using @Enumerated(EnumType.STRING)
are mapped using the .name()
representation of the enum. So in your case: KITE
, BOARD
, ...
Because you are saving them as Kite
, Board
and so on, Hibernate ORM cannot find the enum for each String value.
I think you need to use an @AttributeConverter
:
@Converter
public static class RepairTypeConverter
implements AttributeConverter<RepairType, String> {
public Character convertToDatabaseColumn( RepairType value ) {
if ( value == null ) {
return null;
}
return value.getType();
}
public RepairType convertToEntityAttribute( String value ) {
if ( value == null ) {
return null;
}
return RepairType.fromType( value );
}
}
Where getType()
and fromType()
are used to convert from the enum to the type and vice-versa.
Then you can map the attribute with:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="repair_Type", discriminatorType = DiscriminatorType.STRING)
public abstract class Repair {
...
@Convert( converter = RepairTypeConverter.class )
@Column(name = "repair_Type)
private RepairType repairType;
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.