users@glassfish.java.net

Glassfish w/TopLink JPA cascade subsequent delete issue

From: <glassfish_at_javadesktop.org>
Date: Mon, 21 Jun 2010 15:56:11 PDT

I'm trying to remove a parent item and remove all the child items via cascade, and it WORKS the first time I run it. Afterwards, however, I get a constraint violation thrown from MYSQL.

Here is the relationship within the Reservation class:
@OneToMany(cascade = CascadeType.ALL, mappedBy = "reservation")
    private Collection<Flt2resv> flt2resvCollection;

A Flt2resv maps a Flight to a Reservation. Fairly straightforward.

This works the first time:
1) Create a reservation with flights
2) Remove the reservation, and therefore, remove the Flt2Resv entries

However, after I remove a reservation the first time, and I subsequently create a reservation with flights, the new reservation does not seem to have any Flt2resv objects. They are in the database, however, such that when I try to remove the reservation, I get a constraint error via Mysql.

This is how to reproduce this error currently:
1) Create a reservation with flights
2) Successfully remove the reservation + Flt2Resv entries
3) Create another reservation with flights
4) Try to remove the reservation, but this time the Reservation object doesn't have any Flt2Resv
5) Mysql Exception is thrown because of the Flt2Resv constraint

error:
Caused by: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`imagineairTestDB/flt2resv`, CONSTRAINT `flt2resv_ibfk_2` FOREIGN KEY (`resv_id`) REFERENCES `reservation` (`resv_id`))

I've tried the following:
a) Calling .refresh for the Reservation upon retrieving it from the database
b) Calling .flush() on the EntityManager after each change

For the record, I can redeploy the EAR and then delete the newly-created Reservation with no problem. It's as if the EntityManager isn't picking up changes - or it's keeping some sort of state.

Any help would be appreciated.

Thanks-
Steven
[Message sent by forum member 'steven_landers']

http://forums.java.net/jive/thread.jspa?messageID=475268