我有以下域名图层:
产品:
@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..."
}
但是我得到一个例外:
|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
有关如何将每个产品的标记添加到我的表中的任何建议吗?
感谢您的回答!
答案 0 :(得分:2)
而不是使用
new Tag(basic_product_id: 1...
尝试按照
的方式做一些事情BasicProduct basicProduct1 = new BasicProduct(title: "Product1", ...
...
new Tag(basicProduct: basicProduct1...