gpt4 book ai didi

grails - 如何在grails中建立一对一的双向关系?

转载 作者:行者123 更新时间:2023-12-02 14:21:33 25 4
gpt4 key购买 nike

我有两个域类,并且希望它们之间具有一对一的BIDIRECTIONAL关系。我写:


class Person {

Book book;

String name
Integer age
Date lastVisit

static constraints = {
book unique: true // "one-to-one". Without that = "Many-to-one".
}

}


class Book {

String title
Date releaseDate
String ISBN

static belongsTo = [person:Person] // it makes relationship bi-directional regarding the grails-docs
}


So, i want to have bi-directional, i could NOT find link from Book to Person in generated SQL:


CREATE TABLE `book` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`version` bigint(20) NOT NULL,
`isbn` varchar(255) NOT NULL,
`release_date` datetime NOT NULL,
`title` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1

那么,那意味着它不是双向的吗?如何做双向?

最佳答案

checkout hasOne属性,在其中定义hasOne和belongsTo的类取决于您要存储FK的位置,请检查有关hasOne的此grails文档:
http://www.grails.org/doc/latest/ref/Domain%20Classes/hasOne.html

关于grails - 如何在grails中建立一对一的双向关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4529911/

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