5

I have similar problem like this [Hibernate Exception: Unknown name value for enum class

But in my case,

Unable to filter, so returning non filtered results.Unknown name value for enum class com.xxxx.enums.Status: DELIVERED
java.lang.IllegalArgumentException: Unknown name value for enum class com.xxxx.enums.Status: DELIVERED
    at org.hibernate.type.EnumType.nullSafeGet(EnumType.java:128)
    at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:109)
    at org.hibernate.type.AbstractType.hydrate(AbstractType.java:104)


@Enumerated(value = EnumType.STRING)
@Column(name = "status", length = 10)
@AuditableField
private Status status;

public enum ReleaseStatus {
     DL("Delivered"),
}

Everything seems fine, still I am getting that exception.

Community
  • 1
  • 1
RaceBase
  • 18,428
  • 47
  • 141
  • 202

2 Answers2

11

You have the String DELIVERED in your table. And this string is supposed to be the name() of one of the ReleaseStatus instances. And ReleaseStatus doesn't have any instance named DELIVERED. The only one you posted is named DL.

So what should be in the table is DL not DELIVERED. Or you should rename your enum instance to DELIVERED, to match what is stored in the database table.

You could define a custom Hibernate user type and use it for this enum as well, so that when getting "DELIVERED" from the database, Hibernate finds the enum instance constructed with this value (and ignoring the case). But storing the correct value from the start looks like a betteridea to me.

CSchulz
  • 10,882
  • 11
  • 60
  • 114
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
10

I prefer defining a custom converter like:

    @Column
    @Convert(converter = StatusFirmaDocumentoConverter.class)  <<<<< :)
    @AuditableField
    private Status status;

(note: do not include the @Enumerated attribute) and creating a converter to process the enumerator value like:

public class CustomConverter implements AttributeConverter<Status, String> {

    @Override
    public String convertToDatabaseColumn(Status attribute) {
        return attribute.getValue() ;
    }

    @Override
    public Status convertToEntityAttribute(String dbData) {
        return  StatusFirmaDocumento.fromString(dbData);
    }

}

yeah, it's a shame that you can't tell to hibernate "translate DL to DELIVERED" and viceversa

Josh M.
  • 26,437
  • 24
  • 119
  • 200
Alfredo M
  • 568
  • 3
  • 7
  • 26
  • 6
    Might be useful for someone else, but I left the Enumerated with the Convert and hibernate ignored the converter. Make sure to only have the Convert! – River Sep 09 '16 at 21:17