gpt4 book ai didi

class - Grails新域实例返回null

转载 作者:行者123 更新时间:2023-12-02 14:37:56 25 4
gpt4 key购买 nike

我有一个看起来像这样的域类:

class Offerbyuser {
Number offerPrice
Number minHours

static constraints = {}
}

然后在 Controller 中,我这样做:
def offer = new Offerbyuser(offerPrice:1, minHours:3)

offer始终为NULL。为什么?我是否缺少明显的东西?

更新:这样就可以了,但是我发现不起作用的是之后的方法。
user.addToOutgoingOffers(offer)

用户与Offerbyuser域类具有一对多关系:
class User {
static hasMany = [outgoingOffers:Offerbyuser]
}

我收到此错误:

groovy.lang.MissingMethodException:没有方法签名:twitter4j.UserJSONImpl.addToOutgoingOffers()适用于参数类型:(test.Offerbyuser)值:[按用户提供-价格:1,鸣叫时间:3]

最佳答案

您的代码对我来说很好。我认为您的 Controller 类可能与域类不在同一个程序包中,并且您可能不包括域类。 Grails找不到您的"new"类,但是由于Groovy是一种动态语言,因此不会抛出错误。

请尝试以下方法:

在 Realm 类

package test

class Offerbyuser {
...
}

在 Controller 中:
package test

class TestController {
...
def doSomething = {
def offer = new Offerbyuser(offerPrice:1, minHours:5)
}
}

关于class - Grails新域实例返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6012102/

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