【发布时间】:2016-10-03 17:56:44
【问题描述】:
在将现有应用程序从 Grails 2.5 迁移到 3.1 时,我遇到了双向一对一关系的奇怪问题。
想象一个带有 User 和 Employee 对象的简单模型。 User 代表通用用户帐户。并非所有用户都是员工,但所有员工都是用户。此外,员工有对经理、主管等的引用(也是用户实例)。用户是关系的拥有者。
class User {
Employee employee
static mappedBy = [employee: "user"]
static hasOne = [employee: Employee]
static constraints = {
employee(nullable:true)
}
}
class Employee {
User user // represents employee's own account, a bi-directional one-to-one
User supervisor // represents a supervisor
static belongsTo = [user: User]
static constraints = {
user(unique:true)
supervisor(nullable:true)
}
}
升级到 Grails 3 后的问题是,在创建模式下,这会导致员工表的 supervisor_id 列生成为 NOT NULL,而在 Grails 2 中它可以为空(只有 user_id 不为空) )。
我使用 Grails 3.1.12 和 3.2.0 对此进行了测试,两者的行为相同。我在我的域类声明中做了什么愚蠢的事情吗?我尝试了多种映射来实现与 Grails 2.5 中相同的行为,但没有运气。在某些情况下,我什至在关系的双方都获得了一个外键......
【问题讨论】:
标签: grails grails-orm