0

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!

2
  • I'm gonna guess that you haven't created the service that persists the sale, you have created the database tables, the persistence pojos but nothing that tells java how to persist the pojos int he database. This is a project I did some months ago about jpa and stuff, check the classes inside dani/java/examenuf6/controller github.com/nagarz/ExamenM03UF6 Commented Jun 15, 2015 at 12:58
  • I edited the question with the service code to create a Sale. Commented Jun 15, 2015 at 13:05

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

What do you mean with merge on sale entity, do you have any sample?
I prefer to set it after because I'm testing the idea of change the database. So I have created the method update of SaleService and in this method I test if the entity exists and if so, I call the saveAndFlush from repository. The problem is that it isn't creating the Vendor. I'll change the question to put this method. See the update.
@Leandro try to remove Cascade annotation and add CascadeType.MERGE to ManyToOne annotation. There is no point in mixing JPA and hibernate annotations.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.