您可能需要使用mappedBy來解釋用戶和列表中的字段是如何排列的。以下是我編寫的一些允許用戶創作多個列表的域,但只能將其中一個設置爲「主要」。有幾個額外的可空約束,所以你可以使用腳手架的用戶界面,而不會陷入雞與雞蛋的情況。
class User {
String name
FooList primaryList
static hasMany = [authoredLists: FooList]
static mappedBy = [primaryList: 'primaryOwner', authoredLists: 'author']
static constraints = {
primaryList nullable: true, unique: true
authoredLists nullable: true
}
String toString() { name }
}
我命名這個類「FooList」只是爲了避免與標準清單類的困惑:
class FooList {
static belongsTo = [author: User, primaryOwner: User]
static constraints = {
primaryOwner nullable: true, display: false
}
}
可以列表「的hasMany」用戶? – 2011-03-10 03:30:27
@長列表只屬於一個用戶。 – cripox 2011-03-11 21:08:13