JPA Entitypersister пытается обновить «грязное поле» - PullRequest
0 голосов
/ 22 апреля 2020

Я пытаюсь сохранить сущность JPA с отношением один ко многим, однако мне бы хотелось, чтобы отношение onetomany не обновлялось.

Следующий код работает, но меня раздражает предупреждение:

12:22:06.497 [pool-1-thread-6] WARN o.h.p.entity.AbstractEntityPersister - HHH000502: The [goal] property of the [fri.so.success.domain.entity.SuccessLogEntity] entity was modified, but it won't be updated because the property is immutable.

Хотя я удалил установщик из сущности. Есть ли способ избавиться от этого предупреждения? Как исключить поле цели, чтобы оно стало «грязным» в соответствии с сохранением сущности JPA?

Сущность:

package fri.so.success.domain.entity;

import java.time.LocalDate;
import java.util.Objects;
import java.util.Optional;
import javax.annotation.Nullable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

@Entity
@Table(name = "SuccessLog")
public class SuccessLogEntity {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  private Long userId;

  private Long goalId;

  private String wentWells;

  private String betterWhat;

  private String betterHow;

  private String notes;

  @OneToOne
  @JoinColumn(name = "goalId", referencedColumnName = "id", insertable = false, updatable = false)
  private GoalEntity goal;

  @NotNull
  private LocalDate onDate;

  public SuccessLogEntity() {
  }

  public SuccessLogEntity(Long userId, Long goalId, String wentWells, String betterWhat,
      String betterHow, String notes, LocalDate onDate) {
    this.onDate = Objects.requireNonNull(onDate);
    this.userId = userId;
    this.goalId = goalId;
    this.wentWells = wentWells;
    this.betterWhat = betterWhat;
    this.betterHow = betterHow;
    this.notes = notes;
  }

  public SuccessLogEntity(Long id, Long userId, Long goalId, String wentWells,
      String betterWhat, String betterHow, String notes, LocalDate onDate) {
    this.onDate = Objects.requireNonNull(onDate);
    this.id = id;
    this.userId = userId;
    this.goalId = goalId;
    this.wentWells = wentWells;
    this.betterWhat = betterWhat;
    this.betterHow = betterHow;
    this.notes = notes;
  }

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public Long getUserId() {
    return userId;
  }

  public void setUserId(Long userId) {
    this.userId = userId;
  }

  public Long getGoalId() {
    return goalId;
  }

  public void setGoalId(Long goalId) {
    this.goalId = goalId;
  }

  public String getWentWells() {
    return wentWells;
  }

  public void setWentWells(String wentWells) {
    this.wentWells = wentWells;
  }

  public String getBetterWhat() {
    return betterWhat;
  }

  public void setBetterWhat(String betterWhat) {
    this.betterWhat = betterWhat;
  }

  public String getBetterHow() {
    return betterHow;
  }

  public void setBetterHow(String betterHow) {
    this.betterHow = betterHow;
  }

  public String getNotes() {
    return notes;
  }

  public void setNotes(String notes) {
    this.notes = notes;
  }

  public LocalDate getOnDate() {
    return onDate;
  }

  public void setOnDate(LocalDate onDate) {
    this.onDate = onDate;
  }

  public Optional<GoalEntity> getGoal() {
    return Optional.ofNullable(goal);
  }
}

интерфейс

public interface SuccessLogCrudRepository extends CrudRepository<SuccessLogEntity, Long> {
    Optional<SuccessLogEntity> find(Long id, Long userId);
    LinkedList<SuccessLogEntity> findAll(Long userId);
}

Служба

    return fromEntity(this.successLogCrudRepository.update(
        new SuccessLogEntity(id, userId, goalId, String.join("\n", wentWells), betterWhat, betterHow, notes, onDate))
    );
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...