1

I have mapped a column in Mysql DB to enum in java. But there are invalid entries in the table column (created manually) and which is not in the enum I created in java. When I try to load the entries I get the below exception. Is it possible to suppress this exception i.e. set the enum to 'null' when you have a invalid entry in the column in the Database?

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
  • can't the database be fixed, set null value for invalid values ? Commented Oct 18, 2013 at 12:13
  • It can be fixed but it involves lot of manual work and I have to also ensure it doesen't break other applications which use the DB. Hence it would be super helpful if there is a way in hibernate to suppress these exceptions and set it to null? Commented Oct 18, 2013 at 12:19

2 Answers 2

1

You have to specify your own hibernate type for this enum. Let's assume that you have enum

enum SomeEnum {A, B, C}

Create a custom type for it SomeEnumType implements UserType, ParameterizedType, you will have to implement a lot of methods, the one you are interested in is:

  @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;
  }

In the end you map the column to the field of your entity:

@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;
Sign up to request clarification or add additional context in comments.

Comments

1

I had a similar problem even though my field was correctly annotated with @Enumerated.

Issue was as follows - the schema DDL applied to the database defined the column type to be CHARACTER(5) whereas in the annotated hibernate entity it was defined as follows which defaults to VARCHAR.

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

I changed the database def in Postgres to be a VARCHAR for customEnumType. You can also try to change the metadata for the column by defining it as some other type and making the same change at the db level.

In a nutshell, make sure your database table column definition matches up to what hibernate expects.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.