0
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

SArya
  • 43
  • 6
  • 1
    Use `@Converter` annotation and implement the required converter – XtremeBaumer Mar 19 '19 at 14:49
  • this will work if you put { "status" : "NOT_FOUND" } however you can convert if you want to use string "created" or "RNF" or also you can create a Sql Enum Type if you are using Postgresql. – Jonathan JOhx Mar 19 '19 at 14:55
  • 1
    Possible duplicate of [Map enum in JPA with fixed values?](https://stackoverflow.com/questions/2751733/map-enum-in-jpa-with-fixed-values) – Peter Šály Mar 19 '19 at 16:46

1 Answers1

0

yes, you can achieve the same by using @Enumerated(EnumType.ORDINAL).

See these examples.

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

mapping-enum-types-with-hibernate-annotations

Alien
  • 15,141
  • 6
  • 37
  • 57