房子属于所有者?

时间:2016-10-01 20:46:14

标签: grails gorm

想知道“belongsTo”是否是写方式来表示所有权关系,其中始终存在父对象,但父级可以切换。

即如果所有者域是

class Owner {
  String name

}

House应该是:

class House {
   String address
   Owner owner
}

class House {
   String address
   static belongsTo = [owner: Owner]
}

我想要实现的是双向1:1,我可以访问owner.house或house.owner,并且可以在不删除旧所有者的情况下更改所有者。

2 个答案:

答案 0 :(得分:0)

without deleting the old owner.

所以你有两个选择 - 到目前为止最简单的,当前方法实际上错误的地方应该是Should House be:可能是这个吗?

class House {
   String address
   static hasMany = [owner:Owner]
   Owner currentOwner
}

有了这样的关系,你说房子里有许多主人。所以owner1 owner2 ..它还有一个当前的所有者,即所有者本身。 每次添加新条目时,都会将其添加到所有者集合house.addToOwner(owner),然后设置currentOwner=owner的上次/最新更新。

答案 1 :(得分:0)

如果你想导航两个实体,并且关系是1:1,你可以使用hasOne

class Owner {
  String name
  static hasOne = [house: House]
}

class House {
   String address
   static belongsTo = [owner: Owner]
}
相关问题