[英]Not persisted entity with Hibernate
I have created this code: 我创建了以下代码:
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("");
}
Also, sale is in cascade with vendor: 此外,销售与供应商是级联的:
@ManyToOne(cascade={CascadeType.PERSIST,CascadeType.REFRESH}, targetEntity = Vendor.class)
@Cascade({
org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.PERSIST
})
private Vendor vendor;
The saleService has this code: 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);
}
It's also annoted as @Service
. 它也被标注为@Service
。 The repository is an Interface that implements JpaRepository<Sale, Long>
. 该存储库是实现JpaRepository<Sale, Long>
的接口。
I'm getting an Error saying that the name
property of Vendor, that must not be null, is null. 我收到一条错误消息,说Vendor的name
属性必须为null,不能为null。
Thanks! 谢谢!
upd Answer, corresponding to the first version of a question was entirely removed - in short, it suggested to flush changes again after entity was updated. 对应于问题的第一个版本的upd Answer已被完全删除-简而言之,它建议在实体更新后再次刷新更改。
Current problem is in mixing two kinds of annotation - first is @ManyToOne
annotation that belongs to JPA specification and second is @Cascade
that is Hibernate specific. 当前的问题在于混合两种注释-第一种是属于JPA规范的@ManyToOne
注释,第二种是Hibernate特有的@Cascade
。
Since you do not do anything Hibernate specific in your example, a solution would be to remove @Cascade
annotation and add CascadeType.MERGE
to @ManyToOne
annotation. 由于您在示例中未执行任何特定于Hibernate的操作,因此一种解决方案是删除@Cascade
批注并将CascadeType.MERGE
添加到@ManyToOne
批注。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.