gpt4 book ai didi

grails - 在gorm数据模型中初始化数据

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

我有以下域层:

产品:

@Resource(uri='/product')
class BasicProduct {

String title
String description
Double price

Date dateCreated
Date lastUpdated

static hasMany = [tag : Tag]

static constraints = {
title(blank: false, unique: true)
description(blank: false)
price(blank: false)//, inList: [5.0,15.0,25.0,50.0,100.0])
}

static mapping = {
autoTimestamp true
}

}

标签:
@Resource(uri='/tag')
class Tag {

String title

static belongsTo = [basicProduct: BasicProduct]

Date dateCreated
Date lastUpdated


static constraints = {
title(blank: false, unique: true)
}

static mapping = {
autoTimestamp true
}

}

在我的 Bootstrap.groovy中,我有以下内容:
class BootStrap {

def init = { servletContext ->
new BasicProduct(title: "Product1", description:"blblblblbalablablalbalbablablablablblabalalbllba", price:5.0).save(failOnError: true)
new BasicProduct(title: "Product2", description:"blblblblbalablablalbalbablablablablblabalalbllba", price:75.0).save(failOnError: true)
new BasicProduct(title: "Product3", description:"blblblblbalablablalbalbablablablablblabalalbllba", price:50.0).save(failOnError: true)

new Tag(basic_product_id: 1 ,title: "analysis").save(failOnError: true)


println "initializing data..."
}

但是我得到一个异常(exception):
|Running Grails application
context.GrailsContextLoader Error initializing the application: Validation Error(s) occurred during save():
- Field error in object 'com.testapp.Product.Tag' on field 'basicProduct': rejected value [null]; codes [com.testapp.Product.Tag.basicProduct.nullable.error.com.testapp.Product.Tag.basicProduct,com.testapp.Product.Tag.basicProduct.nullable.error.basicProduct,com.testapp.Product.Tag.basicProduct.nullable.error.com.testapp.Product.BasicProduct,com.testapp.Product.Tag.basicProduct.nullable.error,tag.basicProduct.nullable.error.com.testapp.Product.Tag.basicProduct,tag.basicProduct.nullable.error.basicProduct,tag.basicProduct.nullable.error.com.testapp.Product.BasicProduct,tag.basicProduct.nullable.error,com.testapp.Product.Tag.basicProduct.nullable.com.testapp.Product.Tag.basicProduct,com.testapp.Product.Tag.basicProduct.nullable.basicProduct,com.testapp.Product.Tag.basicProduct.nullable.com.testapp.Product.BasicProduct,com.testapp.Product.Tag.basicProduct.nullable,tag.basicProduct.nullable.com.testapp.Product.Tag.basicProduct,tag.basicProduct.nullable.basicProduct,tag.basicProduct.nullable.com.testapp.Product.BasicProduct,tag.basicProduct.nullable,nullable.com.testapp.Product.Tag.basicProduct,nullable.basicProduct,nullable.com.testapp.Product.BasicProduct,nullable]; arguments [basicProduct,class com.testapp.Product.Tag]; default message [Die Eigenschaft [{0}] des Typs [{1}] darf nicht null sein]
grails.validation.ValidationException: Validation Error(s) occurred during save():
- Field error in object 'com.testapp.Product.Tag' on field 'basicProduct': rejected value [null]; codes [com.testapp.Product.Tag.basicProduct.nullable.error.com.testapp.Product.Tag.basicProduct,com.testapp.Product.Tag.basicProduct.nullable.error.basicProduct,com.testapp.Product.Tag.basicProduct.nullable.error.com.testapp.Product.BasicProduct,com.testapp.Product.Tag.basicProduct.nullable.error,tag.basicProduct.nullable.error.com.testapp.Product.Tag.basicProduct,tag.basicProduct.nullable.error.basicProduct,tag.basicProduct.nullable.error.com.testapp.Product.BasicProduct,tag.basicProduct.nullable.error,com.testapp.Product.Tag.basicProduct.nullable.com.testapp.Product.Tag.basicProduct,com.testapp.Product.Tag.basicProduct.nullable.basicProduct,com.testapp.Product.Tag.basicProduct.nullable.com.testapp.Product.BasicProduct,com.testapp.Product.Tag.basicProduct.nullable,tag.basicProduct.nullable.com.testapp.Product.Tag.basicProduct,tag.basicProduct.nullable.basicProduct,tag.basicProduct.nullable.com.testapp.Product.BasicProduct,tag.basicProduct.nullable,nullable.com.testapp.Product.Tag.basicProduct,nullable.basicProduct,nullable.com.testapp.Product.BasicProduct,nullable]; arguments [basicProduct,class com.testapp.Product.Tag]; default message [Die Eigenschaft [{0}] des Typs [{1}] darf nicht null sein]
at BootStrap$_closure1.doCall(BootStrap.groovy:20)
at grails.util.Environment.evaluateEnvironmentSpecificBlock(Environment.java:308)
at grails.util.Environment.executeForEnvironment(Environment.java:301)
at grails.util.Environment.executeForCurrentEnvironment(Environment.java:277)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Error |
Forked Grails VM exited with error

有什么建议如何为每个产品添加标签到我的表中?

感谢您的回答!

最佳答案

而不是使用

new Tag(basic_product_id: 1...

尝试按照以下方式做点事情
BasicProduct basicProduct1 =  new BasicProduct(title: "Product1", ...

...

new Tag(basicProduct: basicProduct1...

关于grails - 在gorm数据模型中初始化数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21913325/

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