Reputation: 1669
I have the following domain layer:
Product:
@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
}
}
Tags:
@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
}
}
In my Bootstrap.groovy
I have the following :
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..."
}
However I get an 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
Any recommendation how to add a Tag for every product into my table?
I appreciate your answer!
Upvotes: 1
Views: 547
Reputation: 303
Instead of using
new Tag(basic_product_id: 1...
Try doing something along the lines of
BasicProduct basicProduct1 = new BasicProduct(title: "Product1", ...
...
new Tag(basicProduct: basicProduct1...
Upvotes: 2