Grails 访问自动生成的字段和查询
我在ItemController.groovy中有以下代码Grails 访问自动生成的字段和查询,grails,groovy,Grails,Groovy,我在ItemController.groovy中有以下代码 def list = { params.max = 60 def storeYYId = params.id [itemInstanceList: Item.list(params), itemInstanceTotal: Item.count()] } 我在Item.groovy中有以下内容: class Item { String itemName static belongsTo
def list = {
params.max = 60
def storeYYId = params.id
[itemInstanceList: Item.list(params), itemInstanceTotal: Item.count()]
}
我在Item.groovy中有以下内容:
class Item {
String itemName
static belongsTo = [store:Store]
static constraints = {
itemName(blank:false)
storeId()
}
}
这给了我一个错误,因为它告诉我没有storeId属性,但是有,因为store_id是对应数据库中store表的外键
问题1。我如何告诉grails让我访问GORM自动生成的域的属性,比如本例中的id和storeId
问题2。在我的列表操作中,我应该在ItemController.groovy中编写什么代码,以便只检索storeId==storeYYId的项目列表
问题1。我如何告诉grails让我访问
GORM自动生成的域,如中的id和storeId
这个案子
您应该能够以与访问定义的属性完全相同的方式访问自动生成的属性。出现错误的原因是Grails不会自动为项
类生成存储id
属性,它将自动生成的唯一属性是版本
和id
(对于项
和存储
)
问题2。在我的应用程序中,我应该在ItemController.groovy中编写什么代码
列表操作,以便仅检索
storeId==storeYYId
您需要编写HQL或criteria查询来检索这些项。条件查询看起来像这样(未测试)
问题1。我如何告诉grails让我访问
GORM自动生成的域,如中的id和storeId
这个案子
您应该能够以与访问定义的属性完全相同的方式访问自动生成的属性。出现错误的原因是Grails不会自动为项
类生成存储id
属性,它将自动生成的唯一属性是版本
和id
(对于项
和存储
)
问题2。在我的应用程序中,我应该在ItemController.groovy中编写什么代码
列表操作,以便仅检索
storeId==storeYYId
您需要编写HQL或criteria查询来检索这些项。条件查询看起来像这样(未测试)
也可以简单地执行Item.findAllByStore(Store.get(storeId))@Gregg该解决方案肯定更简单,但我希望它会导致发出两个查询,这是我想要避免的。但是,get()总是尝试从缓存中提取。因此,打击可能微不足道。我想你也可以做Item.findAllByStore(Store.load(storeId)),这就足够了,甚至不需要去缓存。也可以简单地做Item.findAllByStore(Store.get(storeId))@Gregg这个解决方案肯定更简单,但我希望它会导致发出两个查询,这是我想要避免的。但是,get()总是尝试从缓存中提取。因此,打击可能微不足道。我想您也可以完成Item.findAllByStore(Store.load(storeId)),这就足够了,甚至不需要去缓存。
// Get all items that have storeId = 6
def storeId = 6
def items = Item.withCriteria {
store {
eq('id', storeId)
}
}