Reputation: 2439
I couldn't delete entitys from db. I have two class one of them is receipt and other is serving. Receipt has
@ElementCollection
@OneToMany(cascade={CascadeType.ALL}, orphanRemoval=true)
public List<Serving> servings;
And i'm tring to delete a serving from a controller.
Serving serving = Serving.findById(servID);
serving.delete();
The result is;
Execution exception PersistenceException occured : org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
Upvotes: 1
Views: 1635
Reputation: 3059
Something like:
Receipt receipt = serving.getReceipt();
receipet.setServings(receipt.getServings().remove(serving));
Since the Receipt is the OO owner of its Servings, from an OO perspective Receipt must know when a Serving is deleted.
Maybe write a finder on Receipt for either a Serving or a servingId.
(Are you sure Servings shouldn't know about which Receipt it belongs to? - the orphanRemoval seems to indicate that a Serving really belongs to one and only one Receipt)
Upvotes: 4
Reputation: 691635
Read the rest of the stack trace, and you should find the name of the constraint that has been violated. You probably have some other entity referencing the serving you try to delete (i.e. some other row having a foreign key to the serving row you try to delete).
So, to be able to delete this entity, you must make sure that all entities referencing it through a foreign key don't reference it anymore. How to do that depends on which entities these are, how their relation with Serving is implemented, and what you want to do: just remove the association, or delete the referencing entity as well.
Upvotes: 2