0
votes

I have two domain objects A and B.

A can be associated with many Bs but I do not want any save cascade from A to B.

I'm thinking of defining the hasMany relationship form A to B, but then setting a cascade behavior. Any ideas?

This is an example of my domain objects:

class A{

static hasMany = [bees:B] }

class B{ }

4

4 Answers

1
votes

If you do not wish for GORM to manage the save/updates for your collection simply don't use hasMany. Instead treat it as a simple HashSet property.

1
votes

Why not use 'belongsTo' on B, and don't declare anything on A? This way you'll get the foreign key to 'A', but operations on 'A' won't affect 'B'. You lose a bit of convenience, but can still easily look up all 'B' by 'A'. I actually prefer this because I don't need to worry about lazy loading gotchas and hibernate going and loading all the 'B's when I'm just trying to add one (assuming you don't need that functionality).

class A{}

class B{
  static belongsTo = [your_a:A]
 }

 //get your B's for a given A
  B.findAllByA(your_A_instance, ...paging, etc...)
0
votes

You can define your own cascading behavior in the static mappings block of your Domain class.

See here: http://grails.org/doc/latest/ref/Database%20Mapping/cascade.html

0
votes

If you are saying what I think your are saying then what you are talking about is not cascading. A simple outline of your classes would be helpful. If you have an instance of A which is associated with many instances of B, then all instance of B which reference the instance of A in question are referencing the exact same object. I had the same problem and asked a similar question here. Basically your options are:

1.) Clone the instance of A whenever it changes or whenever you deem appropriate.

2.) Create new fields in your B class which will hold the values of A you are concerned with.

Both approaches have their advantages and disadvantages, but for me option 2 proved to be the better choice.