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

标签 grails grails-orm grails-domain-class domain-data-modelling

我是 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/

相关文章:

grails - 命令对象和 hasman

mysql - Grails 域类外键映射

hibernate - 扩展域类导致奇怪的表布局和不可访问的字段

intellij-idea - 带有@GrailsCompileStatic 注解的Grails GORM 类在静态映射闭包表、版本、autoTimestamp 中显示为未解析符号

grails - 根据 hasman 关系中的最新记录搜索记录

Grails 使用 spring-security-core-3.0.6+ 重定向注销后

Grails - 子类中的唯一约束

Grails Quartz 插件在第 8 次执行时卡住

exception - 如何在事务中从 Grails 中的数据库错误中恢复

json - 向 Grails 发送日期时的 GORM 默认日期格式