Reputation: 14737
I have a simple enum as follows:
public enum Tax {
NONE(10), SALES(20), IMPORT(30);
private final int value;
private Tax(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
@Entity
public Person {
.........
private TAX tax = TAX.NONE;
public TAX getTax() {
return tax;
}
public void setTax(Tax tax) {
this.tax = tax;
}
.............
}
When saving a Person object in database via JPA, the value saved in the database is 0 for the object's tax field with the value being TAX.NONE.
Why not 10 saved in the database? How can I let JPA save 10 for TAX.NONE in database via JPA?
Thanks!
Upvotes: 0
Views: 94
Reputation: 77226
JPA stores the identity of the enum value in the database, either its index (ordinal) or its name (string). There's no built-in way to retrieve the correct enum instance from a field on the enum, and multiple instances could have the same field value. If you really want to store a secondary field value in the database, you could write your own converter for your persistence provider.
Upvotes: 1