AgentMime
AgentMime
JCHJava Community | Help. Code. Learn.
Created by AgentMime on 9/18/2024 in #java-help
Java Hibernate with Negative Enums
I'm using Hibernate for JPA for persisting objects to my database. One class has four possible statuses that I want to represent with an enum, and I want to use a negative status for DELETED for consistency with previous classes:
public enum Status {
PENDING (0)
APPROVED(1)
DECLINED(2)
DELETED(-1)

private int numVal;

Status(int numVal) {
this.numVal = numVal;
}

public int getNumVal() {
return numVal;
}
}
public enum Status {
PENDING (0)
APPROVED(1)
DECLINED(2)
DELETED(-1)

private int numVal;

Status(int numVal) {
this.numVal = numVal;
}

public int getNumVal() {
return numVal;
}
}
However, Hibernate doesn't like this and is throwing an exception:
...
Caused by: java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 4
at [email protected]//org.hibernate.type.descriptor.java.EnumJavaTypeDescriptor.fromOrdinal(EnumJavaTypeDescriptor.java:76)
at [email protected]//org.hibernate.metamodel.model.convert.internal.OrdinalEnumValueConverter.toDomainValue(OrdinalEnumValueConverter.java:38)
...
...
Caused by: java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 4
at [email protected]//org.hibernate.type.descriptor.java.EnumJavaTypeDescriptor.fromOrdinal(EnumJavaTypeDescriptor.java:76)
at [email protected]//org.hibernate.metamodel.model.convert.internal.OrdinalEnumValueConverter.toDomainValue(OrdinalEnumValueConverter.java:38)
...
Is there a simple way to tell Hibernate to allow the negative Enum value? Or do I have to use non-negative values?
13 replies