gpt4 book ai didi

grails - Grails BootStrap:无方法签名:* .addTo *适用

转载 作者:行者123 更新时间:2023-12-02 15:26:45 25 4
gpt4 key购买 nike

我有两个域类:用户

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
}
}

和个人资料:
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
}
}

一个配置文件只能属于一个用户,而一个用户只能拥有(或属于?)一个配置文件。当我尝试在当前设置中以 BootStrap.groovy创建对象时,收到一条错误消息,指出 addTo()方法不存在。我真的不知道我在做什么错。这就是我在 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)

有人可以指出错误吗?我敢肯定这很傻。

最佳答案

根据您的要求,需要严格的双向一对一关系:

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

域类中主要需要进行三处修改:

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

static constraints = {
profile unique: true
}

//Profile.groovy
User user

以上是双向一对一关系。创建它们时,您不再需要 addTo*了。
Profile arunProfile = new Profile(firstName: 'Arun', lastName: 'Allamsetty')

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

关于grails - Grails BootStrap:无方法签名:* .addTo *适用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26351208/

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