0

I am getting started with Hibernate, and I am getting the following error for an entity which is supposed to have an enum type.

java.lang.IllegalArgumentException: No enum const class app.entity.ObjType.
java.lang.Enum.valueOf(Enum.java:214)
    org.hibernate.type.EnumType.nullSafeGet(EnumType.java:125)
    org.hibernate.type.CustomType.nullSafeGet(CustomType.java:109)
    org.hibernate.type.AbstractType.hydrate(AbstractType.java:104)
    org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2283)

I have the following enum type defined:

package app.entity;

@Entity
class Example {
    @Enumerated(EnumType.STRING)
    private ObjType type;
}

And the defined Enum class is as follows:

package app.entity;

public enum ObjType {
    typeA,
    typeB
}

What am I doing wrong? Also do set and get methods also have to be annotated with "@Enumerated(EnumType.STRING)" ?

I would appreciate any help. Thanks

Loke
  • 249
  • 1
  • 6
  • 14
  • Any chance the project that is calling hibernate's `hydrate()` doesn't actually have the `ObjType` enum in it's classpath somehow? Maybe it is an earlier version or something? – Gray Dec 09 '11 at 16:41

2 Answers2

0

It looks pretty close. Try Using uppercase names for your Enum values to start with. Here's a similar post that may also help. It talks about Hibernate 3.2+ being required.

Enumerations in Hibernate

Come back if its still not clear

Community
  • 1
  • 1
Brad
  • 15,186
  • 11
  • 60
  • 74
  • I have already defined it the way it's explained there. It doesn't help me. Why is it complaining of missing Enum class which is already defined? It happens when I do a query on the entity. – Loke Dec 09 '11 at 16:31
  • Check that your values in the database do not have extra whitespace ( leading or trailing spaces). And they are the same case as your Enum values. Can you clarify when this exception is occuring? – Brad Dec 09 '11 at 16:43
  • Do you create the DB schema or is it created by the Hibernate? Check that the datatype in the DB does not add any trailing / leading spaces. – WeMakeSoftware Dec 09 '11 at 16:52
0

I think you have an empty value in the database which it is trying to load and it can't find an enum value for the empty value. Note the extra . at the end of the error message

app.entity.ObjType.

where it has tried to concat "" with the enum class.

Mike Q
  • 22,839
  • 20
  • 87
  • 129