Hibernate异常:java.lang.IllegalArgumentException:枚举类的未知名称值[]

时间:2013-10-18 12:09:11

标签: java mysql hibernate enums hibernate-mapping

我已将Mysql DB中的列映射到java中的枚举。但是表列中存在无效条目(手动创建),而不是我在java中创建的枚举。当我尝试加载条目时,我得到以下异常。是否可以抑制此异常,即在数据库中的列中包含无效条目时将枚举设置为“null”?

java.lang.IllegalArgumentException: Unknown name value [] for enum class [model.enums.PeriodUnit]
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:2912)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1673)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1605)
at org.hibernate.loader.Loader.getRow(Loader.java:1505)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:713)
at org.hibernate.loader.Loader.processResultSet(Loader.java:943)
at org.hibernate.loader.Loader.doQuery(Loader.java:911)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:312)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2238)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:65)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:674)
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:85)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1849)
at org.hibernate.collection.internal.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:681)
at org.hibernate.engine.internal.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:1030)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:347)
at org.hibernate.loader.Loader.doList(Loader.java:2526)
at org.hibernate.loader.Loader.doList(Loader.java:2512)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2342)
at org.hibernate.loader.Loader.list(Loader.java:2337)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:495)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:357)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1269)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)

2 个答案:

答案 0 :(得分:1)

您必须为此枚举指定自己的休眠类型。我们假设您有枚举

enum SomeEnum {A, B, C}

为它创建一个自定义类型SomeEnumType implements UserType, ParameterizedType,你必须实现很多方法,你感兴趣的是:

  @Override
  public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
      throws HibernateException, SQLException {
    Object result = null;
    try {
      String name = rs.getString(names[0]);
      if (!rs.wasNull()) {
        result = Enum.valueOf(clazz, name);
      }
    } catch (Exception e) {
      // result = null; // Here you handle incorrect database value
    }
    return result;
  }

最后,您将列映射到实体的字段:

@Column(name = "some_enum", columnDefinition = "enum('A','B','C')")
@Type(type = "com.somepackage.SomeEnumType", parameters = @Parameter(name = "type", value = "com.somepackage.SomeEnumType"))
private SomeEnum someEnum;

答案 1 :(得分:1)

即使我的字段被@Enumerated正确注释,我也遇到了类似的问题。

问题如下 - 应用于数据库的模式DDL将列类型定义为CHARACTER(5),而在带注释的hibernate实体中,它定义如下,默认为VARCHAR。

@Column
@Enumerated(EnumType.String)
private CustomEnumType customEnumType; 

我将Postgres中的数据库def更改为customEnumType的VARCHAR。您还可以尝试通过将列定义为其他类型并在数据库级别进行相同更改来更改列的元数据。

简而言之,请确保您的数据库表列定义与hibernate期望的匹配。