我在 RentalUnit 和 Review 之间有多对多的关系(入住多个出租单元的客人可能会有评论)。从 RentalUnit 到 Review 的 Delete 有级联,但从 Review 到 RentalUnit 没有级联
在进行测试时,我发现 GORM 会话中存在以下不一致
def review2 = new Review(rentalUnits: [rentalUnit], ...., isApproved: false).save(flush: true) review2.addToRentalUnits(rentalUnit2)
“rentalUnit2”对象将关联到“review2”,而“rentalUnit”则没有。
在初始化时或通过 addTo* 传递 RentalUnit 对象时如何确保一致的会话?
ps 这里是完整的代码 class Review { String submitBy String content String dateReceived boolean isApproved
final static DateFormat DATEFORMAT = DateFormat.getDateInstance(DateFormat.MEDIUM)
static belongsTo = RentalUnit
static hasMany = [rentalUnits: RentalUnit]
static mapping = {
rentalUnits cascade: "none"
}
static constraints = {
submittedBy blank: false, size: 3..50
content blank: false, size: 5..255
dateReceived blank: false, size: 11..12, validator: {
try{
Date date = DATEFORMAT.parse(it)
return DATEFORMAT.format(date) == it
}catch(ParseException exception){
return false
}
}
rentalUnits nullable: false
}
}
class RentalUnit { String name String 昵称 Address address static hasMany = [reviews:Review]
static mapping = {
reviews cascade: "all-delete-orphan"
}
static constraints = {
name blank: false, unique: true
nickname blank: false
}
}