[英]Not persisted entity with Hibernate
我創建了以下代碼:
Sale sale = new Sale();
saleService.create(sale);
Vendor vendor = new Vendor("name");
Sale updatedSale = saleService.findById(sale.getId());
updatedSale.setVendor(vendor);
try {
saleService.update(updatedSale);
} catch (EntityNotFoundException ex) {
System.err.println("");
}
此外,銷售與供應商是級聯的:
@ManyToOne(cascade={CascadeType.PERSIST,CascadeType.REFRESH}, targetEntity = Vendor.class)
@Cascade({
org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.PERSIST
})
private Vendor vendor;
saleService具有以下代碼:
@Transactional
public Sale create(Sale entity) {
Sale created = entity;
saleRepository.saveAndFlush(created);
return created;
}
@Transactional(rollbackFor = EntityNotFoundException.class)
public Calibration update(Calibration entity) throws EntityNotFoundException {
if (!calibrationRepository.exists(entity.getId())) {
throw new EntityNotFoundException();
}
return calibrationRepository.saveAndFlush(entity);
}
它也被標注為@Service
。 該存儲庫是實現JpaRepository<Sale, Long>
的接口。
我收到一條錯誤消息,說Vendor的name
屬性必須為null,不能為null。
謝謝!
對應於問題的第一個版本的upd Answer已被完全刪除-簡而言之,它建議在實體更新后再次刷新更改。
當前的問題在於混合兩種注釋-第一種是屬於JPA規范的@ManyToOne
注釋,第二種是Hibernate特有的@Cascade
。
由於您在示例中未執行任何特定於Hibernate的操作,因此一種解決方案是刪除@Cascade
批注並將CascadeType.MERGE
添加到@ManyToOne
批注。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.