为什么 grails 不通过 bootstrap 填充数据
why is grails not populating data via bootstrap
我有以下bootstrapclass
import shoppingsolutionproject.Category;
import shoppingsolutionproject.Item
class BootStrap {
def dataSource
def init = { servletContext ->
new Category(description: 'Car').save()
new Category(description: 'Truck').save()
new Item(productNumber:1, name:"First Product", description: "This is the first product I'm adding for testing",
shippingCost: 4.99, url: '/first', retailPrice: 19.99, salePrice: 16.99, category: 'Car').save()
new Item(productNumber:2, name:"Second Product", description: "This is the second product I'm adding for testing",
shippingCost: 4.99, url: '/second', retailPrice: 19.99, category: 'Truck').save()
new Item(productNumber:3, name:"First/Second Product", description: "This is the first/second(so third) product I'm adding for testing",
shippingCost: 4.99, url: '/second', retailPrice: 17.99, category: 'Truck').save()
}
def destroy = {
}
}
虽然我启动 grails 时,我没有预先填充类别或项目...知道为什么吗?
您很可能遇到验证错误。
验证这一点的最快方法是将 failOnError: true
添加到您的 .save()
,如下所示:.save(failOnError: true)
。
这应该会告诉您很多由于验证错误而失败的事情。
我有以下bootstrapclass
import shoppingsolutionproject.Category;
import shoppingsolutionproject.Item
class BootStrap {
def dataSource
def init = { servletContext ->
new Category(description: 'Car').save()
new Category(description: 'Truck').save()
new Item(productNumber:1, name:"First Product", description: "This is the first product I'm adding for testing",
shippingCost: 4.99, url: '/first', retailPrice: 19.99, salePrice: 16.99, category: 'Car').save()
new Item(productNumber:2, name:"Second Product", description: "This is the second product I'm adding for testing",
shippingCost: 4.99, url: '/second', retailPrice: 19.99, category: 'Truck').save()
new Item(productNumber:3, name:"First/Second Product", description: "This is the first/second(so third) product I'm adding for testing",
shippingCost: 4.99, url: '/second', retailPrice: 17.99, category: 'Truck').save()
}
def destroy = {
}
}
虽然我启动 grails 时,我没有预先填充类别或项目...知道为什么吗?
您很可能遇到验证错误。
验证这一点的最快方法是将 failOnError: true
添加到您的 .save()
,如下所示:.save(failOnError: true)
。
这应该会告诉您很多由于验证错误而失败的事情。