枚举类

时间:2016-06-27 15:30:17

标签: hibernate enums

我得到了下面的堆栈跟踪    线程" main"中的例外情况java.lang.IllegalArgumentException:枚举类的未知名称值[NC-17] [com.ecw.pojo.Rating]     at org.hibernate.type.EnumType $ NamedEnumValueMapper.fromName(EnumType.java:467)     at org.hibernate.type.EnumType $ NamedEnumValueMapper.getValue(EnumType.java:452)     在org.hibernate.type.EnumType.nullSafeGet(EnumType.java:107)     在org.hibernate.type.CustomType.nullSafeGet(CustomType.java:127)     在org.hibernate.type.AbstractType.hydrate(AbstractType.java:106)     在org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2969)     在org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1695)     在org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1627)     在org.hibernate.loader.Loader.getRow(Loader.java:1514)     在org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:725)     在org.hibernate.loader.Loader.processResultSet(Loader.java:952)     在org.hibernate.loader.Loader.doQuery(Loader.java:920)     在org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)     在org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:324)     在org.hibernate.loader.Loader.loadCollection(Loader.java:2263)     在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)     在org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1893)     在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)     在java.io.PrintStream.println(PrintStream.java:821)     在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
    }
}

在实体中如下

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

1 个答案:

答案 0 :(得分:2)

Hibernate不关心您的自定义值属性。它使用枚举的名称:compile('com.google.firebase:firebase-core:9.0.2') { exclude group: 'com.google.android.gms.measurement' } ,而不是NC17

如果您想使用NC-17,则需要一个Hibernate自定义类型的JPA属性转换器。