How to map enum(with fields) to entity Column?

时间:2019-03-19 14:47:29

标签: java hibernate spring-boot jpa enums

public enum TransStatus{
NOT_FOUND(12,"RNF"),
CREATED(43,"created");
private Integer statusCode;
private String statusCodeString;

TransStatus(Integer statusCode,String statusCodeString){
this.statusCode = statusCode;
this.statusCodeString = statusCodeString;
}
public Integer getStatusCode() {
return statusCode;
}
public String getStatusCodeString() {
return statusCodeString;
}
}

with Entity class as follows:

@Enumerated(EnumType.STRING)
@Column(name="STATUS")
private TransStatus status;

is there any way that I can map TransStatus column in my entity to the Integer value of enum that is status code, for example, if I have Enum value as NOT_FOUND TransStatus value in entity should be 12,

edit please notice i have two values of enum status code , status code string , what we want is only status code, we have already tried with "Ordinal" it prints the integer value of the enum not the statusCode in my example

1 个答案:

答案 0 :(得分:0)

是的,您可以使用@Enumerated(EnumType.ORDINAL)来达到相同的目的。

请参阅这些示例。

map-enum-in-jpa-with-fixed-values

mapping-enum-types-with-hibernate-annotations