Grails 如何在三个域类之间建立多个一对一关系

Grails 如何在三个域类之间建立多个一对一关系,grails,gorm,Grails,Gorm,作为后续问题,我希望有一个用户域类,它与基本配置文件域类具有可选的一对一关系,用户是所有者,可能有也可能没有配置文件。我已经弄明白这部分了。我还想要的是AcademicProfile域类和用户,AcademicProfile作为所有者之间的可选一对一关系,这样AcademicProfile可能有也可能没有用户。当我试图以我第一次一对一关系的方式复制这一点时,它不起作用。这是我的课 class User { String username String password

作为后续问题,我希望有一个
用户
域类,它与
基本配置文件
域类具有可选的一对一关系,
用户
是所有者,可能有也可能没有配置文件。我已经弄明白这部分了。我还想要的是
AcademicProfile
域类和
用户
AcademicProfile
作为所有者之间的可选一对一关系,这样
AcademicProfile
可能有也可能没有
用户
。当我试图以我第一次一对一关系的方式复制这一点时,它不起作用。这是我的课

class User {

    String username
    String password
    String email
    AcademicProfile academicProfile
    Date dateCreated
    Date lastUpdated

    static hasOne = [basicProfile: BasicProfile]

    static constraints = {
        username size: 3..20, unique: true, nullable: false, validator: { _username ->
            _username.toLowerCase() == _username
        }
        password size: 6..100, nullable: false, validator: { _password, user ->
            _password != user.username
        }
        email email: true, blank: false
        basicProfile nullable: true
    }
}

class BasicProfile extends Profile {

    User user
    Date dateCreated
    Date lastUpdated

}

class AcademicProfile extends Profile {

    String dblpId
    String scholarId
    String website
    Date dateCreated
    Date lastUpdated

    static hasOne = [user: User]
    static hasMany = [publications: Publication]

    static constraints = {
        dblpId nullable: true
        scholarId nullable: true
        website nullable: true, url: true
        publications nullable: true
        user nullable: true
    }
}

class Profile {
    String firstName
    String middleName
    String lastName
    byte[] photo
    String bio

    static constraints = {
        firstName blank: false
        middleName nullable: true
        lastName blank: false
        photo nullable: true, maxSize: 2 * 1024**2
        bio nullable: true, maxSize: 500
    }

    static mapping = {
        tablePerSubclass true
    }
}

当我运行它时,我得到错误:
字段'academicProfile'上的对象'org.academical.User'中的字段错误:拒绝值[null]。我不明白我做错了什么。

您必须为用户类中的
AcademicProfile-AcademicProfile
添加
nullable:true
约束,正如您提到的,您需要AcademicProfile和用户之间的“可选”关系


不过,错误本身是不言自明的,如果不为academicProfile属性提供非null值,就无法创建用户类的实例。

我已经尝试过了。我得到一个错误:
消息:Hibernate操作:无法执行语句;SQL[n/a];“学术_档案_ID”列不允许为NULL;SQL语句:插入用户(id、版本、学术档案id、创建日期、电子邮件、上次更新、密码、用户名)值(空、、、、、、、、、、)[23502-176];嵌套异常为org.h2.jdbc.JdbcSQLException:NULL不允许用于列“学术_PROFILE_ID”;SQL语句:
看看您的数据库结构,显然“学术档案ID”列仍然不是空的constraint@ArunAllamsetty:您能否将更新的域类与约束和生成的db结构共享?