users@glassfish.java.net

JPA Merge and Remove

From: <glassfish_at_javadesktop.org>
Date: Sat, 23 Jun 2007 15:40:12 PDT

Hi,

may an easy question, however i didn't managed to find a solution yet...

I have a Bean with @ManyToOne unidirectional relationship:
May be a garage with cars...

@Entity
public class Garage()
{
@ManyToOne (cascade = CascadeTyp.ALL)
private List <Vehicles> getVehicles(){...}
}

List <Vehicles> = [Motorcycle, Car, Skateboard,...]

So, I persit the data and get three tables, Garage, Vehicles and a JoinTable. The entity bean Garage is detached and unmanaged further on!
Now, I want to remove a car from the garage, so I reduce the car from the list of vehicles.

Merging the Data shall, in my opinion, release the connection between the garage and the car. So the entry of the JoinTable shall disapear! (Which doesn't?!)
Remaining the car in the Vehicle table.

tx.begin;
em.merge(garage);
em.commit;
tx.close

Later I want to remove the car from the List of Vehicles too.

Vehicle car = find(Vehicle.class, vehicleID);

tx.begin;
em.remove(vehicle);
em.commit;
tx.close

Only removing the car without having it in the garage once works fine, otherwise I get the Error:

Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.0 (Build b45-beta3 (05/03/2007))): oracle.toplink.essentials.exceptions.DatabaseException
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: DELETE on table 'VEHICLE' caused a violation of foreign key constraint 'BSCDTTRGTCPLCPLNGD' for key (202). The statement has been rolled back.
Error Code: 20000
Call: DELETE FROM VEHICLE WHERE (VEHICLEID = ?)
        bind => [202]
Query: DeleteObjectQuery(Vehicle Car)
oracle.toplink.essentials.exceptions.DatabaseException.sqlException(DatabaseException.java:296)
oracle.toplink.essentials.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:639)
oracle.toplink.essentials.internal.databaseaccess.DatabaseAccessor.executeNoSelect(DatabaseAccessor.java:688)
...
MainProgramManagement.ProgramMain.removeData(ProgramMain.java:830)

So, if I have a look at the JoinTable, there is still the connection between the Garage and the Car!? (the foreign key constraint 'BSCDTTRGTCPLCPLNGD' refers to the JoinTable)

Why can't I get the Car out of the Garage? What do I wrong? I spend a few hours reading and searching, however I don't get it...

Thanks,

Marcel

(Using Java6, Derby 10.2, Netbeans 5.5 and TopLink V2 b45)
[Message sent by forum member 'javari' (javari)]

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