Sunny
Sunny

Reputation: 308

Unknown name value [] for enum class

I got below stacktrace Exception in thread "main" java.lang.IllegalArgumentException: Unknown name value [NC-17] for enum class [com.ecw.pojo.Rating] at org.hibernate.type.EnumType$NamedEnumValueMapper.fromName(EnumType.java:467) at org.hibernate.type.EnumType$NamedEnumValueMapper.getValue(EnumType.java:452) at org.hibernate.type.EnumType.nullSafeGet(EnumType.java:107) at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:127) at org.hibernate.type.AbstractType.hydrate(AbstractType.java:106) at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2969) at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1695) at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1627) at org.hibernate.loader.Loader.getRow(Loader.java:1514) at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:725) at org.hibernate.loader.Loader.processResultSet(Loader.java:952) at org.hibernate.loader.Loader.doQuery(Loader.java:920) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:324) at org.hibernate.loader.Loader.loadCollection(Loader.java:2263) at org.hibernate.loader.collection.plan.LegacyBatchingCollectionInitializerBuilder$LegacyBatchingCollectionInitializer.initialize(LegacyBatchingCollectionInitializerBuilder.java:105) at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:693) at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:92) at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1893) at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:555) at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:260) at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:551) at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:140) at org.hibernate.collection.internal.PersistentSet.toString(PersistentSet.java:316) at java.lang.String.valueOf(String.java:2994) at java.io.PrintStream.println(PrintStream.java:821) at util.TestMapping.main(TestMapping.java:35)

My enum class as below

public enum Rating {

    G("G"),
    PG("PG"),
    PG13("PG-13"),
    R("R"),
    NC17("NC-17");

    private String value;

    private Rating(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String toString() {
        return this.value; //will return , or ' instead of COMMA or APOSTROPHE
    }
}

And in entity as below

@Column(name = "rating")
@Enumerated(EnumType.STRING)
private Rating rating;

Upvotes: 2

Views: 6372

Answers (1)

JB Nizet
JB Nizet

Reputation: 691685

Hibernate doesn't care about your custom value attribute. It uses the name of the enum: NC17, not NC-17.

If you want to use NC-17, you need a Hibernate custom type of a JPA attribute converter.

Upvotes: 2

Related Questions