Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Exception - Unknown name value

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.

like image 460
RaceBase Avatar asked Jun 21 '14 05:06

RaceBase


2 Answers

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.

like image 63
JB Nizet Avatar answered Oct 17 '22 06:10

JB Nizet


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

like image 39
Alfredo M Avatar answered Oct 17 '22 04:10

Alfredo M