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?
11 Replies
JavaBot
JavaBot3mo 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-chan3mo 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
dan1st3mo 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
AgentMimeOP3mo ago
What do you mean by "user" type?
Kyo-chan
Kyo-chan3mo 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
AgentMimeOP3mo ago
Fair enough lol
dan1st
dan1st3mo ago
what a fun game 😂 - but beware the dangers of learning something on the way
JavaBot
JavaBot3mo 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.
AgentMime
AgentMimeOP3mo ago
This was a bit verbose but it works great, thanks!
JavaBot
JavaBot3mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
JavaBot
JavaBot3mo ago
Post Closed
This post has been closed by <@406648669748592650>.
Want results from more Discord servers?
Add your server