嵌套 hasOne 实体的 GORM 查询条件
GORM query criteria for nested hasOne entity
我正在尝试通过 属性 子实体创建 GORM 条件查询过滤。所以有这样的实体:
class PaymentEntry {
static hasOne = [category: PaymentCategory]
static constraints = {
category(nullable: true)
}
// other stuff
}
class PaymentCategory {
static hasMany = [payments: PaymentEntry]
// other stuff
}
现在我正在尝试 select 具有特定类别的 PaymentEntries。我正在尝试这样的事情:
def c = PaymentEntry.createCriteria()
def res = c {
'in'("category", categories)
}
categories
这里是 PaymentCategory
个实体的列表,select 早些时候。
不幸的是,这失败了。 Grails 抛出 NoSuchMethodException。
你应该有inList。
试试这个:
def res = c {
inList("category", categories)
}
有很多问题。 hasOne
用于一对一 associations,但实际上您有一对多。所以第一步是修复关联,可能是这样的:
class PaymentEntry {
static belongsTo = [category: PaymentCategory]
static constraints = {
category(nullable: true)
}
// other stuff
}
class PaymentCategory {
static hasMany = [payments: PaymentEntry]
// other stuff
}
接下来,一旦您拥有条件实例,您需要调用其方法之一,例如 list()
,以构建并执行您的查询。
def c = PaymentEntry.createCriteria()
def res = c.list {
'in'("category", categories)
}
同一事物的更短版本是...
def res = PaymentEntry.withCriteria {
'in'("category", categories)
}
您可以使用 in()
和 inList()
,只要您像以前那样引用 in
,因为它是 Groovy 关键字。您可以阅读有关条件查询的更多信息 here.
我正在尝试通过 属性 子实体创建 GORM 条件查询过滤。所以有这样的实体:
class PaymentEntry {
static hasOne = [category: PaymentCategory]
static constraints = {
category(nullable: true)
}
// other stuff
}
class PaymentCategory {
static hasMany = [payments: PaymentEntry]
// other stuff
}
现在我正在尝试 select 具有特定类别的 PaymentEntries。我正在尝试这样的事情:
def c = PaymentEntry.createCriteria()
def res = c {
'in'("category", categories)
}
categories
这里是 PaymentCategory
个实体的列表,select 早些时候。
不幸的是,这失败了。 Grails 抛出 NoSuchMethodException。
你应该有inList。 试试这个:
def res = c {
inList("category", categories)
}
有很多问题。 hasOne
用于一对一 associations,但实际上您有一对多。所以第一步是修复关联,可能是这样的:
class PaymentEntry {
static belongsTo = [category: PaymentCategory]
static constraints = {
category(nullable: true)
}
// other stuff
}
class PaymentCategory {
static hasMany = [payments: PaymentEntry]
// other stuff
}
接下来,一旦您拥有条件实例,您需要调用其方法之一,例如 list()
,以构建并执行您的查询。
def c = PaymentEntry.createCriteria()
def res = c.list {
'in'("category", categories)
}
同一事物的更短版本是...
def res = PaymentEntry.withCriteria {
'in'("category", categories)
}
您可以使用 in()
和 inList()
,只要您像以前那样引用 in
,因为它是 Groovy 关键字。您可以阅读有关条件查询的更多信息 here.