简体   繁体   English

JPA 存储具有 NULL 值的 Java 枚举

[英]JPA store a Java Enum with a NULL value

I'm using SpringBoot and JPA.我正在使用 SpringBoot 和 JPA。

Into my entity I have a status field mapped as enum.在我的实体中,我有一个状态字段映射为枚举。 I would like to map e specific enum-value as NULL.我想将 map 指定为 NULL 的枚举值。

I tried something like this:我试过这样的事情:

public enum Status {

        DELETED(null),
        ACTIVE(1);

        private final Integer type;

        Status(Integer type) {
            this.type = type;
        }

        public int getStatusValue() {
            return type;
        }

        public static Status from(int value) {
            return Status.values()[value];
        }
    }

But this approach do not works properly.但是这种方法不能正常工作。

When I try to set DELETED value for my model and I try to save on the DB the status value is 0.当我尝试为我的 model 设置 DELETED 值并尝试保存在数据库上时,状态值为 0。

Is there a way to set DELETED status and to have NULL value on the database directly?有没有办法设置 DELETED 状态并直接在数据库上设置 NULL 值?

You need to use an attribute convertor for this use case - have a look at this question that shows the steps to follow - Spring Data JPA not using AttributeConverter in Spring Boot Application您需要为此用例使用属性转换器 - 查看显示要遵循的步骤的问题 - Spring Data JPA not using AttributeConverter in Z38008DD81C2F4D7985ECF6E0CE8AF1D1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM