【问题标题】:JPA store a Java Enum with a NULL valueJPA 存储具有 NULL 值的 Java 枚举
【发布时间】:2021-08-03 15:18:48
【问题描述】:

我正在使用 SpringBoot 和 JPA。

在我的实体中,我有一个状态字段映射为枚举。我想将 e 特定的枚举值映射为 NULL。

我尝试过这样的事情:

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];
        }
    }

但是这种方法不能正常工作。

当我尝试为我的模型设置 DELETED 值并尝试保存在数据库中时,状态值为 0。

有没有办法设置DELETED状态并直接在数据库上设置NULL值?

【问题讨论】:

    标签: java spring-boot jpa enums


    【解决方案1】:

    您需要为此用例使用属性转换器 - 看看这个显示要遵循的步骤的问题 - Spring Data JPA not using AttributeConverter in Spring Boot Application

    【讨论】:

      猜你喜欢
      • 2016-11-10
      • 1970-01-01
      • 1970-01-01
      • 2015-03-02
      • 2010-12-13
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      相关资源
      最近更新 更多