Reputation: 1170
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:
@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
. The repository is an Interface that implements JpaRepository<Sale, Long>
.
I'm getting an Error saying that the name
property of Vendor, that must not be null, is null.
Thanks!
Upvotes: 0
Views: 690
Reputation: 11619
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.
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.
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.
Upvotes: 1