Hi everybody ! I'm trying to globally disable casc...
# questions
o
Hi everybody ! I'm trying to globally disable cascade save and cascade validation for Domain contained in another one class A{ } class B{ A a } B b = B.get(1) b.save() //I don't want a saved I tried this in Application.groovy but it doesn't work
Copy code
grails.gorm.default.mapping = {
    '*'(cascadeValidate: 'none', cascade:'none')
}
Ideas ? Thanks Grails 6.2.0
j
I'm not sure if you can turn it off globally, but If it's associated to the session, it's going to validate & save it if valid when the transaction finishes. I don't think you can turn off the saving behavior unless you don't associate it to the session. They have discard() to make sure it's not attached to the session. Can't you just call discard() on B?
I also assume that A doesn't have hasMany B defined? If it does, it's associated to the session because the parent's save() call.
o
I did not use belongsTo neither hasMany statements
j
How is B getting associated to the session then?
If it just has a reference to A and no other domain object has a reference to B, it should have never saved.
the cascade mapping is only valid if you use the hasMany, etc on A
o
Yes "a" is just a reference in B, but it's saved when I save B, that's weird for me...
j
Oh, so it's cascading up, not down.
Isn't it a hasOne by default if defined as a property?
The detailed documentation for these behaviors are here: https://gorm.grails.org/latest/hibernate/manual/index.html
Looks like there's an option for cascadeValidate, and none even is a possible value. the example they give is "dirty" as a default.
o
Isn't it a hasOne by default if defined as a property?
Do you know how to disable this hasOne by default ?
j
I don't think you can. Gorm is designed to do that stuff automatically.
😢 1
o
> Looks like there's an option for cascadeValidate, and none even is a possible value. the example they give is "dirty" as a default. But it does not work for me...
j
have you tried with 'dirty'?
o
No, only with 'none' because it is in the doc too... but I could try to see
j
I've often found it useful to attach the debugger and check the hibernate session contents - maybe the save isn't happening because of the domain object. It may be dirty / changed for another reason and since it's in the session it's being saved
If i recall correctly, you can inject the sessionFactory or uses Holders.applicationContext.getBean(SessionFactory) from the debugger, and then dig down into the session to find what's there.
o
Thanks, I will dig 😉
👍 1