Grails BootStrap:没有方法签名:* .addTo *适用 [英] Grails BootStrap: No signature of method: *.addTo* applicable

查看:187
本文介绍了Grails BootStrap:没有方法签名:* .addTo *适用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个域类:用户

  class用户{

字符串用户名
字符串密码
字符串电子邮件
日期dateCreated
日期lastUpdated

// static belongsTo = [profile:Profile]

static constraints = {
用户名大小:3..20,unique:true,nullable:false,验证器:{_username - >
_username.toLowerCase()== _username
}
密码大小:6..100,可为空:false,验证器:{_password,user - >
_password!= user.username
}
电子邮件电子邮件地址:true,blank:false
//配置文件可为空:true
}
}

和配置文件:

  class Profile {

字符串名字
字符串中间名
字符串姓氏
字节[]照片
日期dateCreated
日期lastUpdated

static属性= [用户]

静态约束= {
firstName空白:false
middleName可空值:true
姓氏空白:false
photo nullable:true,maxSize:2 * 1024 ** 2
}
}

配置文件只能属于一个用户,而用户只能拥有(或属于?)只有一个配置文件。当我尝试在当前设置中创建 BootStrap.groovy 中的对象时,出现错误,提示 addTo()方法不存在。我真的不知道我做错了什么。这是我在 BootStrap.groovy 中创建它们的方式:

 用户arun = new User(用户名:'arun',密码:'password',电子邮件:'arun@email.com')save(failOnError:true)
Profile arunProfile = new Profile(firstName:'Arun', lastName:'Allamsetty')。addToUser(arun).save(failOnError:true)

指出错误。我确信这很愚蠢。

解决方案

严格的双向一对一关系是您所要求的: p>

配置文件只能属于一个用户,而用户只能拥有一个配置文件

在域类中主要需要三个修改:

  // User.groovy 
static hasOne = [profile:Profile]

static constraints = {
profile unique:true
}

//Profile.groovy
用户用户

以上是双向一对一的关系。在创建它们时,您不需要 addTo *

 用户名:'arun',密码:'密码',
电子邮件地址:'arun',密码:'密码',


$ b用户arun = 'arun@email.com',
profile:arunProfile).save()


I have two domain classes: User

class User {

    String username
    String password
    String email
    Date dateCreated
    Date lastUpdated

//  static belongsTo = [profile: Profile]

    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
//      profile nullable: true
    }
}

and Profile:

class Profile {

    String firstName
    String middleName
    String lastName
    byte[] photo
    Date dateCreated
    Date lastUpdated

    static belongsTo = [User]

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

A profile can belong to only one user and a user can have (or belong to?) only one profile. When I try to create the objects in BootStrap.groovy in the current setup I get an error saying that the addTo() method does not exist. I don't really know what I am doing wrong. This is how I am creating them in BootStrap.groovy:

User arun = new User(username: 'arun', password: 'password', email: 'arun@email.com').save(failOnError: true)
Profile arunProfile = new Profile(firstName: 'Arun', lastName: 'Allamsetty').addToUser(arun).save(failOnError: true)

Can someone please point out the mistake(s). I am sure it's silly.

解决方案

A strict bi-directional one-one relationship is required as you have requested for:

A profile can belong to only one user and a user can have (or belong to?) only one profile

Three modifications are mainly required in domain classes:

//User.groovy
static hasOne = [profile: Profile]

static constraints = { 
    profile unique: true
}

//Profile.groovy
User user

Above is a bi-directianl one-one relationship. You do not need addTo* anymore while creating each of them.

Profile arunProfile = new Profile(firstName: 'Arun', lastName: 'Allamsetty')

User arun = new User(username: 'arun', password: 'password', 
                     email: 'arun@email.com', 
                     profile: arunProfile).save()

这篇关于Grails BootStrap:没有方法签名:* .addTo *适用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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