I'm making a script using some already created (not by me) domain classes from grails.
开发者_如何转开发class Person extends OAP {
static hasMany = [addresses: Address]
(...)
}
class Address {
static belongsTo = [oap: OAP]
(...)
}
class OAP has no reference to Address.
So I was trying to do:
p.save()
a.oap = p
println a.oap
a.save()
with p being Person and a being Address, but although it prints the correct person on the console, the reference is not saved on the address table (oap_id stays null)
P.S.: It may not be the best relationship set-up in grails, but that's what I have to work with
Try p.addToAddresses(a)
and then p.save()
. It should save both p
and a
.
See http://grails.org/doc/latest/guide/5.%20Object%20Relational%20Mapping%20%28GORM%29.html#5.2.4%20Sets,%20Lists%20and%20Maps
I have no idea how GORM will behave in this situation because you have essentially entered into this weird zone where you have a unidirectional hasMany
on Person which results in a SAVE-UPDATE
cascade behavior from Person and a NONE
on Address. Then you also have a unidirectional one-to-one between Person and OAP which results in an ALL
cascade behavior on the OAP side, and a NONE
on the Address side. So I'm not sure what to even expect here. You need to fix the relationship to either:
- Make it so OAP and not Person
hasMany = [address:Address]
- Make it so Address
belongsTo = [person:Person]
Or, give some additional explanation as to what you're trying to do in your relationship and we can go from there.
Please try with this, it resolved my problem
p.addToAddresses(a);
p.save(flush:true)
精彩评论