如何在三个域类之间建立多个一对一关系 [英] How to have multiple One-to-One relationships between three domain classes

查看:103
本文介绍了如何在三个域类之间建立多个一对一关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为这个的后续问题,我想要一个用户 domain类,它与 BasicProfile 域类具有可选的一对一关系,用户是所有者其中可能有也可能没有配置文件。我有这方面的想法。我还想要的是 AcademicProfile 域类与 User 之间的可选一对一关系, AcademicProfile 作为所有者,因此 AcademicProfile 可能有也可能没有 User 。当我尝试按照我做第一对一关系的方式进行复制时,它不起作用。

As a follow up question to this, I want to have a User domain class which has an optional one-to-one relationship with BasicProfile domain class, User being the owner which may or may not have a profile. I have this part figured out. What I also want is an optional one-to-one relationship between the AcademicProfile domain class and the User, AcademicProfile being the owner, such that an AcademicProfile may or may not have a User. When I try to replicate this the way I did the first one-to-one relationship, it does not work. Here are my classes.

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.academic.User'中的字段错误:被拒绝的值[null]; 。我不明白我在做什么错误。

When I run it, I get the error: Field error in object 'org.academic.User' on field 'academicProfile': rejected value [null];. I don't understand what I am doing wrong.

推荐答案

您必须添加 nullable:对于 AcademicProfile academicProfile 在用户类中的真实约束,您提到您需要AcademicProfile& User。

You'll have to add nullable:true constraint for AcademicProfile academicProfile in User class as you mentioned that you need an 'optional' relationship between AcademicProfile & User.

这个错误本身就是一个自我解释,你不能创建一个User类的实例,但不提供academicProfile属性的非空值。

The error itself is self explanatory though, that you can't create a User class's instance, without providing the academicProfile property a non-null value.

这篇关于如何在三个域类之间建立多个一对一关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆