ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ingmar Lötzsch <iloetz...@asci-systemhaus.de>
Subject Re: Updating/Deleteing Objects With One-To-Many Relationships
Date Wed, 25 Feb 2009 08:14:38 GMT
> So, if I have
> 
> class Person {
>     int id;
>     String name;
>     List<Car> cars;
> }
> 
> class Person {
>     int id;
>     String name;
> }

Have you really two versions of class Person?

> And I have 3 tables
> 
> person (
>     id,
>     name
> )
> 
> car (
>     id,
>     name
> )
> 
> person_x_car (
>     person_id
>     car_id
> )
> 
> How is it that I go about
> 
> 1) Updating a Person who has changes to their cars

There is no UPDATE statement to achieve this. You can DELETE all rows in
person_x_car and then INSERT the recent pairs (person_id, car_id), if
there are no dependencies on the table person_x_car. Otherwise, or in
case you want to log the changes, you have to identify the disjoined
respectively added cars and DELETE/INSERT the appropriate rows.

> 2) Deleting a Person and subsequently deleting the relevant rows in
> person_x_car

If your DBMS support DELETE ON CASCADE you can just delete the person row.

-- ALTER TABLE person_x_car DROP CONSTRAINT fk_person_x_car_person;
ALTER TABLE person_x_car
ADD CONSTRAINT fk_person_x_car_person FOREIGN KEY (person_id)
REFERENCES person (id) ON DELETE CASCADE;

Otherwise you have to iterate over the car list and delete each join
before deleting the person.

Mime
View raw message