gpt4 book ai didi

Grails 域类 : hasOne, 有许多没有属于

转载 作者:行者123 更新时间:2023-12-04 13:09:23 25 4
gpt4 key购买 nike

我是 Grails 的新手。
我可以使用“hasOne”或“hasMany”而不使用“belongsTo”到另一个域类吗?

提前致谢。

最佳答案

是的你可以。请参阅 Grails 文档中的示例:http://grails.org/doc/2.3.8/guide/GORM.html#manyToOneAndOneToOne

hasMany(没有belongsTo)示例来自文档:

A one-to-many relationship is when one class, example Author, has many instances of another class, example Book. With Grails you define such a relationship with the hasMany setting:


class Author {
static hasMany = [books: Book]
String name
}

class Book {
String title
}

In this case we have a unidirectional one-to-many. Grails will, by default, map this kind of relationship with a join table.



hasOne(没有belongsTo)示例来自文档:

Example C


class Face {
static hasOne = [nose:Nose]
}
class Nose {
Face face
}

Note that using this property puts the foreign key on the inverse table to the previous example, so in this case the foreign key column is stored in the nose table inside a column called face_id. Also, hasOne only works with bidirectional relationships.

Finally, it's a good idea to add a unique constraint on one side of the one-to-one relationship:


class Face {
static hasOne = [nose:Nose]
static constraints = {
nose unique: true
}
}

class Nose {
Face face
}

关于Grails 域类 : hasOne, 有许多没有属于,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23905638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com