Java 休眠异常 - 未知名称值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24338629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate Exception - Unknown name value
提问by Reddy
I have similar problem like this [Hibernate Exception: Unknown name value for enum class
我有类似的问题 [ Hibernate Exception: Unknown name value for enum class
But in my case,
但就我而言,
Unable to filter, so returning non filtered results.Unknown name value for enum class com.xxxx.enums.Status: DELIVERED
java.lang.IllegalArgumentException: Unknown name value for enum class com.xxxx.enums.Status: DELIVERED
at org.hibernate.type.EnumType.nullSafeGet(EnumType.java:128)
at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:109)
at org.hibernate.type.AbstractType.hydrate(AbstractType.java:104)
@Enumerated(value = EnumType.STRING)
@Column(name = "status", length = 10)
@AuditableField
private Status status;
public enum ReleaseStatus {
DL("Delivered"),
}
Everything seems fine, still I am getting that exception.
一切似乎都很好,但我仍然遇到了那个例外。
采纳答案by JB Nizet
You have the String DELIVERED
in your table. And this string is supposed to be the name()
of one of the ReleaseStatus
instances. And ReleaseStatus
doesn't have any instance named DELIVERED. The only one you posted is named DL
.
你DELIVERED
的表中有字符串。这个字符串应该是name()
其中一个ReleaseStatus
实例的。并且ReleaseStatus
没有任何名为 DELIVERED 的实例。您发布的唯一一个名为DL
。
So what should be in the table is DL
not DELIVERED
. Or you should rename your enum instance to DELIVERED
, to match what is stored in the database table.
所以应该在表中的DL
不是DELIVERED
. 或者您应该将您的枚举实例重命名为DELIVERED
,以匹配存储在数据库表中的内容。
You could define a custom Hibernate user type and use it for this enum as well, so that when getting "DELIVERED" from the database, Hibernate finds the enum instance constructed with this value (and ignoring the case). But storing the correct value from the start looks like a betteridea to me.
您可以定义自定义 Hibernate 用户类型并将其用于此枚举,以便在从数据库获取“DELIVERED”时,Hibernate 找到使用此值构造的枚举实例(并忽略大小写)。但是从一开始就存储正确的值对我来说似乎是一个更好的主意。
回答by Alfredo M
I prefer defining a custom converter like:
我更喜欢定义一个自定义转换器,如:
@Column
@Convert(converter = StatusFirmaDocumentoConverter.class) <<<<< :)
@AuditableField
private Status status;
(note: do notinclude the @Enumerated
attribute) and creating a converter to process the enumerator value like:
(注意:不包括@Enumerated
属性)并创建一个转换器来处理枚举值,如:
public class CustomConverter implements AttributeConverter<Status, String> {
@Override
public String convertToDatabaseColumn(Status attribute) {
return attribute.getValue() ;
}
@Override
public Status convertToEntityAttribute(String dbData) {
return StatusFirmaDocumento.fromString(dbData);
}
}
yeah, it's a shame that you can't tell to hibernate "translate DLto DELIVERED" and viceversa
是的,很遗憾你不能告诉休眠“将DL翻译为DELIVERED”,反之亦然