java Hibernate/JPA 错误 - 无法识别枚举中的某些字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6432933/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:51:57  来源:igfitidea点击:

Hibernate/JPA bug - Not recognizing some strings in a enum

javahibernateexceptionenums

提问by lucasarruda

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, NOKor NAP. NOKor NAPworks 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:

我在一个类中有一个枚举,由 Hibernate 映射。映射字段之一是 enum 类型,它具有以下值OKNOKNAP 之一NOKNAP按预期工作,但是当该类的字段设置为“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)

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 OKto OK2, it works correctly. _OKalso 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.

如果我将OK更改为OK2,则它可以正常工作。_OK也可以。就我而言, 'OK' 不是保留名称(就像在这种情况下使用new 一样),因为它可以正确编译。

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.

到现在为止,我解决这个问题的方法是修改枚举并在数据库中存储' _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;
    }
}

BUG REPORT:

错误报告:

A bug report has been filled.

一个错误报告已经充满

回答by Marcelo

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.

您遇到的问题是,在您的数据库中,您有除 OK、NOK、NAP 之外的其他值,并且当您检索记录时,是在遇到异常时,而不是在持久化时。

From your Exception com.a.b.c.d.Class$Status: OK2it seems as your database has that value and hence the java.lang.IllegalArgumentException: Unknown name value for enum classexception.

从您的异常com.a.b.c.d.Class$Status: OK2看来,您的数据库具有该值,因此是java.lang.IllegalArgumentException: Unknown name value for enum class异常。

Check your table for invalid values, remove/correct them, and try again.

检查您的表格中是否存在无效值,删除/更正它们,然后重试。

回答by Vlad

As a another workaround you can try providing an exact column definition to have a VARCHAR column:

作为另一种解决方法,您可以尝试提供一个精确的列定义来拥有一个 VARCHAR 列:

@Column(columnDefinition = "VARCHAR(3)")
// @Column(columnDefinition = "VARCHAR2(3)") // VARCHAR2 for Oracle
@Enumerated(EnumType.STRING)
public Status getStatus() {
    return status;
}

public enum Status {
    OK, NOK, NAP;
}

回答by Vlad

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.

将 HSQLDB 从 1.8 升级到 2.2.6 后,我遇到了完全相同的问题。对于枚举,Hibernate 创建 CHARACTER 类型的列,这是一个固定长度的列(与 VARCHAR 相反)。它的长度可能确定为最长枚举值的长度。Hibernate 可以正确生成 INSERT/UPDATE 语句,但是当从表中读取值时,这些较短的值会附加空格。

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.

因此,HSQLDB 驱动程序似乎没有修剪它们。如果应该的话,我认为应该为 HSQLDB 而不是 Hibernate 提交错误。但是,如果 HSQLDB 的这种行为与 SQL 标准兼容,那么 Hibernate 的 EnumType 应该在读取枚举值时进行修剪。