1

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?

Safari
  • 11,437
  • 24
  • 91
  • 191

1 Answers1

2

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

Mannie
  • 1,608
  • 1
  • 21
  • 33