Hibernate/JPA 错误 - 无法识别枚举中的某些字符串
我在一个类中有一个枚举,由 Hibernate 映射。映射字段之一是枚举类型,具有以下值之一:OK、NOK 或 NAP。 NOK 或 NAP 按预期工作,但是当该类的字段设置为“OK”时,Hibernate 无法映射和检索该值,该值设置为 null
java.lang.IllegalArgumentException: Unknown name value for enum class com.a.b.c.d.Class$Status: OK
at org.hibernate.type.EnumType.nullSafeGet(EnumType.java:113)
:类有:
private Status status;
@JoinColumn(name = "STATUS")
@Enumerated(EnumType.STRING)
public Status getStatus() {
return status;
}
public enum Status {
OK, NOK, NAP;
}
如果我将 OK 更改为 OK2,它可以正常工作。 _OK也有效。 就我而言,“OK”不是保留名称(就像在这种情况下,那个人 使用new),因为它可以正确编译。
谢谢!
更新:
到目前为止,我为解决问题所做的就是修改枚举并将“_OK”存储在数据库中,而不是“OK',如上所示。这不是很好的解决方案,但至少有效。
public enum Status {
_OK("OK"),
NOK("NOK"),
NAP("NAP");
private String desc;
private Status(String desc){
this.desc = desc;
}
public String getDesc(){
return desc;
}
}
错误报告:
I have an enum in a class, mapped by Hibernate. One of the mapped fields is and enum type which has one of the following values OK, NOK or NAP. NOK or NAP works as expected, but when the field the class is set to 'OK', Hibernate fails to map and retrieve the value, which is set to null:
java.lang.IllegalArgumentException: Unknown name value for enum class com.a.b.c.d.Class$Status: OK
at org.hibernate.type.EnumType.nullSafeGet(EnumType.java:113)
The class has:
private Status status;
@JoinColumn(name = "STATUS")
@Enumerated(EnumType.STRING)
public Status getStatus() {
return status;
}
public enum Status {
OK, NOK, NAP;
}
If I change OK to OK2, it works correctly. _OK also works.
As far as i'm concerned 'OK' is not a reserved name (like in this case where the guy uses new) as it compiles correctly.
Thanks!
UPDATE:
Up 'till now, what I did to solve the problem is to modify the enum and store '_OK' in the database instead of 'OK', as shown above. Not very nice solution, but it works at least.
public enum Status {
_OK("OK"),
NOK("NOK"),
NAP("NAP");
private String desc;
private Status(String desc){
this.desc = desc;
}
public String getDesc(){
return desc;
}
}
BUG REPORT:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您遇到的问题是,在您的数据库中,您有 OK、NOK、NAP 以外的值,并且当您检索记录时,是您收到异常时,而不是当您坚持时。
从您的异常
com.abcdClass$Status: OK2
看来,您的数据库具有该值,因此出现java.lang.IllegalArgumentException: Unknown name value for enum class
异常。检查表中是否有无效值,删除/更正它们,然后重试。
The problem you have is that in your database you have values others than OK, NOK, NAP and when you are retrieving the records is when you are getting the exception, not when you are persisting.
From your Exception
com.a.b.c.d.Class$Status: OK2
it seems as your database has that value and hence thejava.lang.IllegalArgumentException: Unknown name value for enum class
exception.Check your table for invalid values, remove/correct them, and try again.
作为另一种解决方法,您可以尝试提供精确的列定义以拥有 VARCHAR 列:
As a another workaround you can try providing an exact column definition to have a VARCHAR column:
将 HSQLDB 从 1.8 升级到 2.2.6 后,我遇到了完全相同的问题。对于枚举,Hibernate 创建 CHARACTER 类型的列,这是一个固定长度的列(与 VARCHAR 相反)。它的长度可能被确定为最长枚举值的长度。 INSERT/UPDATE 语句由 Hibernate 正确生成,但是当从表中读取值时,这些较短的值会附加空格。
因此,HSQLDB 驱动程序似乎没有修剪它们。如果应该的话,我认为应该针对 HSQLDB 而不是 Hibernate 提交该错误。但是,如果 HSQLDB 的这种行为与 SQL 标准兼容,那么 Hibernate 的 EnumType 在读取枚举值时应该进行修剪。
I'm having exactly same problem after upgrading HSQLDB from 1.8 to 2.2.6. For enums Hibernate creates columns of type CHARACTER which is a fixed length column (contrary to VARCHAR). The length of it probably determined as a length of the longest enum value. INSERT/UPDATE statements are generated correctly by Hibernate, but when reading values from the table those shorter values come out appended with white spaces.
So, it seems that HSQLDB driver does not trim them. And if it should, I think the bug should be filed for HSQLDB, not Hibernate. However, if this behaviour of HSQLDB is compatible with SQL standard, then it's the Hibernate's EnumType should do trimming when reading enum values.