这是我的实体:
@Entity
public class BookRisk extends CustomerBaseEntity<BookRisk, BookRiskDTO> {
private String bookRiskData;
private OffsetDateTime externalQueryDate;
@OneToOne
private Cafe cafe;
BookRisk bookRisk = bookRiskDataRepository.findByCafeId(cafe.getId());
if (bookRisk == null) {
return bookRiskDataRepository.save(
new BookRisk(bookRiskData, getExternalQueryDate(bookRiskData), cafe)).getId();
} else {
return bookRiskDataRepository.save(
bookRisk.modifyBookRisk(bookRisk, request.getBookRiskData(),
getExternalQueryDate(bookRiskData))).getId();
}
}
在这里,如果以前没有保存过,我想创建一个新的BookRİskData
对象。不是,我想根据请求设置值,然后再次保存以进行更新。
对于更新,它将创建另一个对象,然后给出错误:
HibernateOptimisticLockingFailureException : Object of class BooktRisk] with identifier [8]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : BookRisk#8] (Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) :
我认为是因为起初我使用
findByCafeId
然后我做
save
但是我不确定。
更新方法:
public BookRisk modifyBookRisk(BookRisk bookRisk, String bookRiskData, OffsetDateTime externalQueryDate) {
bookRisk.setExternalQueryDate(externalQueryDate);
bookRisk.setBookRiskData(bookRiskData);
return bookRisk;
}
我不更改version
或id
。第二次保存时,它也会增加版本。
它还有另一个课程ID。
是因为cafe
吗?
COnstructor:
public BookRisk(String bookRiskData, OffsetDateTime externalQueryDate, Cafe cafe) {
this.createStamp = EntityActivityDateTimeStamp.fromContext();
this.modifyStamp = createStamp.copy();
this.bookRiskData = bookRiskData;
this.externalQueryDate = externalQueryDate;
this.cafe = cafe;
;
}
public BookRisk() {
}