3

Имеется следующая модель: CarService:

@EqualsAndHashCode(callSuper = true)
@Table
@Entity
@Data
@NoArgsConstructor
public class CarService extends BaseEntity {

  @Column
  private String address;
  @OneToMany
  @LazyCollection(LazyCollectionOption.FALSE)
  @Cascade(CascadeType.SAVE_UPDATE)
  private List<PairSparePartCount> sparePartsCount;
  @OneToMany
  @LazyCollection(LazyCollectionOption.FALSE)
  @Cascade(CascadeType.SAVE_UPDATE)
  private List<Master> masters = new ArrayList<>();
  @OneToMany
  @LazyCollection(LazyCollectionOption.FALSE)
  @Cascade(CascadeType.SAVE_UPDATE)
  private List<Request> requestList;
}

PairSparePartCount:

@EqualsAndHashCode(callSuper = true)
@Table
@Entity
@Data
@NoArgsConstructor
public class PairSparePartCount extends BaseEntity {

  @ManyToOne
  @Cascade(CascadeType.SAVE_UPDATE)
  private SparePart sparePart;
  @Column
  private Integer count;
}

SparePart:

@EqualsAndHashCode(callSuper = true)
@Table
@Entity
@Data
@NoArgsConstructor
public class SparePart extends BaseEntity {

  @Column
  private String name;
  @Column
  private String carBrand;
  @Column
  private String carModel;
  @Column
  private Double price;
}

Когда пытаюсь сохранить CarService со списком PairSparePartCount , которых ещё не существует в базе вылетает Exception:

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: by.bsuir.model.PairSparePartCount; nested exception is java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: by.bsuir.model.PairSparePartCount

Необходимо чтобы при сохранении CarService , если какой-то PairSparePart нет в списке она создавалась автоматически, насколько я понимаю это Cascade , но почему не работает SAVE_UPDATE ?

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Посмотрите другие вопросы с метками или задайте свой вопрос.