Как аннотировать окончательное поле id с помощью аннотации @Id в Spring - PullRequest
1 голос
/ 23 февраля 2020

Можно ли аннотировать идентификатор поля с помощью аннотации @Id в Spring, если это поле является окончательным? Хорошо, история в том, что если я удаляю последнее поле для идентификатора и пытаюсь сохранить его в базе данных (в моем случае это mongodb), оно работает без каких-либо проблем, даже если я оставил это поле пустым, оно в любом случае генерируется автоматически, но с финальным оно выдает NullPointerException, если я не заполняю его. Я хотел бы сохранить реализацию сущности, которую вы можете увидеть чуть ниже.

Спасибо за любую помощь заранее, с наилучшими пожеланиями Виктор.

@Document(collection = "posts")
public class PostEntity {
    private final String id;
    private final String title;
    private final String content;
    private final Instant createTime;
    private final Instant updateTime;
    private final Instant deleteTime;
    private final PostTypeEnum postType;

    private PostEntity(Builder builder) {
        this.id = builder.id;
        this.title = builder.title;
        this.content = builder.content;
        this.createTime = builder.createTime;
        this.updateTime = builder.updateTime;
        this.deleteTime = builder.deleteTime;
        this.postType = builder.postType;
    }

    public static Builder builder() {
        return new Builder();
    }

    public String getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public String getContent() {
        return content;
    }

    public Instant getCreateTime() {
        return createTime;
    }

    public Instant getUpdateTime() {
        return updateTime;
    }

    public Instant getDeleteTime() {
        return deleteTime;
    }

    public PostTypeEnum getPostType() {
        return postType;
    }

    public static final class Builder {
        private String id;
        private String title;
        private String content;
        private Instant createTime;
        private Instant updateTime;
        private Instant deleteTime;
        private PostTypeEnum postType;

        private Builder() {
        }

        public PostEntity build() {
            return new PostEntity(this);
        }

        public Builder id(String id) {
            this.id = id;
            return this;
        }

        public Builder title(String title) {
            this.title = title;
            return this;
        }

        public Builder content(String content) {
            this.content = content;
            return this;
        }

        public Builder createTime(Instant createTime) {
            this.createTime = createTime;
            return this;
        }

        public Builder updateTime(Instant updateTime) {
            this.updateTime = updateTime;
            return this;
        }

        public Builder deleteTime(Instant deleteTime) {
            this.deleteTime = deleteTime;
            return this;
        }

        public Builder postType(PostTypeEnum postType) {
            this.postType = postType;
            return this;
        }
    }
}
...