#Java Hibernate with Negative Enums

1 messages · Page 1 of 1 (latest)

plain marsh
#

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;
  }
}

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)
...

Is there a simple way to tell Hibernate to allow the negative Enum value? Or do I have to use non-negative values?

cerulean hazelBOT
#

This post has been reserved for your question.

Hey @plain marsh! 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.

white parcel
#

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.

zinc yacht
plain marsh
white parcel
#

....... New game: before you ask a question like this one you first assume it must have some meaning and you freaking google it

plain marsh
#

Fair enough lol

zinc yacht
cerulean hazelBOT
#

💤 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.

plain marsh
cerulean hazelBOT