Java 如何将枚举类型存储为小写字符串?

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

How to store enum type as lowercase string?

javahibernatejpaenumspersistence

提问by eugenes

My model class (piece):

我的模型类(件):

public class User ... {

    @Enumerated(STRING)
    private Status status;

    ...

    public enum Status {

        ACTIVE,
        INACTIVE;

        @Override
        public String toString() {
            return this.name().toLowerCase();
        }
    }

    ...

    public String getStatus() {
        return status.name().toLowerCase();
    }

    public void setStatus(Status status) {
        this.status = status;
    }
}

As you see above I override toString method, but no effect. Enumeration store in database as ACTIVEor INACTIVE.

正如你在上面看到的,我覆盖了 toString 方法,但没有效果。数据库中的枚举存储为ACTIVEor INACTIVE

P.S. I use hibernate jpa

PS我使用休眠jpa

Thanks for help!

感谢帮助!

P.S.S. I ask because I write REST service that produces json (in json object better use lower case, if I'm not mistake)

PSS 我问是因为我编写了生成 json 的 REST 服务(在 json 对象中最好使用小写,如果我没记错的话)

采纳答案by Kent

write a converterclass, annotated with @Converter, which implements javax.persistence.AttributeConverter<YourEnum, String>. There are two methods:

编写一个converter用 注释的类@Converter,它实现javax.persistence.AttributeConverter<YourEnum, String>. 有两种方法:

public String convertToDatabaseColumn(YourEnum attribute){..}
public YourEnum convertToEntityAttribute(String dbData) {..}

there you can apply your upper/lower case logic.

在那里你可以应用你的大写/小写逻辑。

Later you can annotated your field, for using the given converter.

稍后您可以注释您的字段,以使用给定的转换器。

回答by Abhijith Nagarajan

Best practice is to use the upper-case for enum constants.

最佳做法是对枚举常量使用大写。

I see two options

我看到两个选项

  1. Use lower-case in enum (not a good practice)
  2. Change the getter method (getStatus method) of the bean using this enum to return lower case. (best possible option)
  1. 在枚举中使用小写(不是一个好习惯)
  2. 使用此枚举更改 bean 的 getter 方法(getStatus 方法)以返回小写。(最好的选择)

回答by naXa

Here's a quick and practical example of using AttributeConverter(introduced in JPA 2.1)

这是一个快速实用的使用示例(在 JPA 2.1 中引入)AttributeConverter

Update your enum class:

更新您的枚举类:

public enum Status {
    ACTIVE,
    INACTIVE;

    public String toDbValue() {
        return this.name().toLowerCase();
    }

    public static Status from(String status) {
        // Note: error if null, error if not "ACTIVE" nor "INACTIVE"
        return Status.valueOf(status.toUpperCase());
    }
}

Create attribute converter:

创建属性转换器:

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

@Converter(autoApply = true)
public class StatusConverter implements AttributeConverter<Status, String> {
    @Override
    public String convertToDatabaseColumn(Status status) {
        return status.toDbValue();
    }

    @Override
    public Status convertToEntityAttribute(String dbData) {
        return Status.from(dbData);
    }
}

If autoApplyis set to true, you don't need to add the javax.persistence.Convertannotation to all attributes that shall be converted. Otherwise, apply the converter:

如果autoApply设置为true,则不需要javax.persistence.Convert为所有需要转换的属性添加注解。否则,应用转换器:

import javax.persistence.Convert;
import javax.persistence.Entity;

@Entity
public class User {

    @Convert(converter = StatusConverter.class)
    @Enumerated(STRING)
    private Status status;

    // ... other fields, constructor(s), standard accessors
}