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 org.hibernate@5.3.20.Final-redhat-00001//org.hibernate.type.descriptor.java.EnumJavaTypeDescriptor.fromOrdinal(EnumJavaTypeDescriptor.java:76)
at org.hibernate@5.3.20.Final-redhat-00001//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 org.hibernate@5.3.20.Final-redhat-00001//org.hibernate.type.descriptor.java.EnumJavaTypeDescriptor.fromOrdinal(EnumJavaTypeDescriptor.java:76)
at org.hibernate@5.3.20.Final-redhat-00001//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?
8 Replies
JavaBot
JavaBot3d ago
This post has been reserved for your question.
Hey @AgentMime! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Kyo-chan
Kyo-chan3d ago
Just make it a user type. If you want Hibernate to use an enum automatically, it will just do the simplest and use their ordinal number.
dan1st
dan1st3d ago
Stack Overflow
Hibernate map enum by property value
I'm trying to do the following using Hibernate: When an object is saved to the DB, it is saved as a the value of the enum's name property. When an object is retrieved from the DB, the object reads...
AgentMime
AgentMime3d ago
What do you mean by "user" type?
Kyo-chan
Kyo-chan3d ago
....... New game: before you ask a question like this one you first assume it must have some meaning and you freaking google it
AgentMime
AgentMime3d ago
Fair enough lol
dan1st
dan1st3d ago
what a fun game 😂 - but beware the dangers of learning something on the way
JavaBot
JavaBot3d ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server