【问题标题】:How to retrieve all instance in grails database如何检索grails数据库中的所有实例
【发布时间】:2015-11-10 12:21:08
【问题描述】:

这些是我的域类

GameCategory.groovy

class GameCategory {
String categoryName
String icon
String toString(){
    "${categoryName}"
}
static hasMany = [ list:GameList]
static constraints = {
}

Game.groovy

class Game {
String gameTitle
float gamePrice
String gameDescription
Date releaseDate
float rating
int numberOfRaters
int numberOfReviews
String toString(){
    "${gameTitle}"
}
static hasMany = [list : GameList ]

static constraints = {
}

GameList.groovy

class GameList {
static belongsTo = [game : Game , category : GameCategory]  
static constraints = {
}

我的问题是,我如何检索给定类别作为参数的游戏的所有实例我无法理解 grails 中的 hasMany 和 belongsTo

【问题讨论】:

    标签: grails


    【解决方案1】:
    GameList.findAllByCategory(myCategory).collect{it.game}
    

    您可以使用createCriteria 使其更复杂,但您必须使用别名加入,代码会变得更复杂。

    【讨论】:

    • 你能解释一下it.game中的“it”是什么,collect{it.game}有什么作用吗?
    • 引用collect的闭包参数:mrhaki.blogspot.se/2010/05/…写成GameList.findAllByCategory(myCategory).collect{GameList currentList -> currentList.game}可能更清楚
    • 所以如果我想把所有游戏放在一个列表中我只需要 def sampleList = GameList.findAllByCategory(myCategory).collect{it.game}?
    • 在我尝试之前最后一个问题你从哪里得到“myCategory”?
    • myCategory 是您已有的 GameCategory 并且您想为其查找所有游戏。
    猜你喜欢
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 2019-05-24
    • 2010-09-28
    • 2016-12-28
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    相关资源
    最近更新 更多