Tobias
Tobias

Reputation: 60

Grails GORM many-to-many relationship and deleting of jointable-entries

I have 2 classes Car and Garage. They have a m:n relation, so one car has many garages and one garage many cars.

I have a garage_has_car table in my database, where the relations are stored. I have configured set belongsTo on the car to Garage

static belongsTo = [  Garage ]

If I clear the cars collection on a garage and save, the corresponding entries in the garage_has_car table are deleted.

But if I clear the garage collection on the car and save, the corresponding entries in the garage_has_car table do not get deleted!

How can I achieve this behaviour?

UPDATE 1 (adding relationship definition):

In Garage the relationship is:

cars column:'deskriptor_id',joinTable:'garage_has_cars'

In Cars it is

static belongsTo = [  Garage ]
garages column:'car_id',joinTable:'garage_has_cars'

I've asked this question in IRC too, and someone replied:

(...) grails many to many relationships are always only cascade one direction. also many-to-many relationships in grails are horribly inefficient with how they get mapped in hibernate. don't use them. create a joinclass domain object CarGarage with 1 car and 1 garage. make cars and garages properties of your classes transients and add get and add methods to the car and garage classes which use your joinclass for maintaining the relationship. you also want to disable versioning in the join class here's some info can be found in this presentation http://burtbeckwith.com/blog/files/169/gorm%20grails%20meetup%20presentation.pdf and the notes of it are here: Grails: Many-to-Many without hasMany/belongsTo - instead using native 3NF - Searching full text

I post this quote because it may help someone with the same problem as I have. I will also update this question, once I have time to implement these changes.

Upvotes: 4

Views: 3181

Answers (1)

user800014
user800014

Reputation:

Just answering to remove from the unanswered list.

(...) grails many to many relationships are always only cascade one direction. also many-to-many relationships in grails are horribly inefficient with how they get mapped in hibernate. don't use them. create a joinclass domain object CarGarage with 1 car and 1 garage. make cars and garages properties of your classes transients and add get and add methods to the car and garage classes which use your joinclass for maintaining the relationship. you also want to disable versioning in the join class here's some info can be found in this presentation http://burtbeckwith.com/blog/files/169/gorm%20grails%20meetup%20presentation.pdf and the notes of it are here: Grails: Many-to-Many without hasMany/belongsTo - instead using native 3NF - Searching full text

Upvotes: 2

Related Questions