简体   繁体   中英

JPA store a Java Enum with a NULL value

I'm using SpringBoot and JPA.

Into my entity I have a status field mapped as enum. I would like to map e specific enum-value as 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.

Is there a way to set DELETED status and to have NULL value on the database directly?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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