I have a two classes, Service and Customer, with a many-to-one association from Service to Customer. I want to delete a Customer and any Service that references it. I'm using JPA as the ORM (with Hibernate underneath) attached to a PostgreSQL db.
It'd be great if I could define the association in such a way that deleting the Customer would cascade to the Services that reference it. But, and maybe I'm misunderstanding something, since the association is defined using @ManyToOne in Service the operations would cascade from Service to Customer but not the other way around?
So without the cascading delete, I set out to simply remove all Services that reference the Customer. Seemed easy enough except JPA/Hibernate wants to batch the deletes up and executes them in the wrong order! My code basically queries the Services that reference the Customer, calls entityManager.remove() on each, and then calls entityManager.remove() on the Customer. But when I flush it, I get an exception that the delete from Customer failed due to the foreign key constraint.
Do I really need to try to commit the delete from Service before the delete from Customer? I'd rather not as my transactions are container managed and it'd be a pain in the neck to make it commit.
Thanks, Andy