枚举类型 Hibernate 3
我正在开始使用 Hibernate,并且对于应该具有枚举类型的实体,我收到以下错误。
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)
我定义了以下枚举类型:
package app.entity;
@Entity
class Example {
@Enumerated(EnumType.STRING)
private ObjType type;
}
定义的枚举类如下:
package app.entity;
public enum ObjType {
typeA,
typeB
}
我做错了什么?另外 set 和 get 方法也必须用“@Enumerated(EnumType.STRING)”注释吗?
我将不胜感激任何帮助。 谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您在尝试加载的数据库中有一个空值,并且无法找到该空值的枚举值。注意额外的 .在错误消息的末尾,
它尝试将“”与枚举类连接起来。
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
where it has tried to concat "" with the enum class.
看起来很接近。尝试首先对 Enum 值使用大写名称。这是一篇类似的文章,也可能有帮助。它谈到需要 Hibernate 3.2+。
Hibernate 中的枚举
如果还不清楚请回来
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