Skip to main content

JPA Merge and Remove

3 replies [Last post]
javari
Offline
Joined: 2007-03-13

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 getVehicles(){...}
}

List = [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(car);
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 was edited by: javari

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mf125085
Offline
Joined: 2005-03-29

Hi Marcel,

your relationship is a uni-directional OneToMany, which must be annotated @OneToMany. Uni-directional OneToMany relationships are mapped to a join table by default. After commiting

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

the join table entry should be removed from the database.

chris_delahunt
Offline
Joined: 2005-07-06

Hello,

Not sure how you have a @ManyToOne annotation on a List attribute - shouldn't it be a @OneToMany, as in one Garagehas many Vehicles?

I'd recommend turning the Toplink logging level to finest to see the annotation processing info for your classes to see if its going wrong. Set

in your persistence.xml.

Best Regards,
Chris

samdoyle
Offline
Joined: 2006-12-27

You're lucky you are actually getting entries in the jointable. My OneToMany only creates entries for less then half the records I try to persist leaving most of my data orphaned. :(