gpt4 book ai didi

postgresql - Grails 如何正确加入 withCriteria

转载 作者:行者123 更新时间:2023-11-29 11:35:04 45 4
gpt4 key购买 nike

我在 grails 2.0.3 上有以下声明,效果很好
我想按条件更改的查询

    def result = db.rows('SELECT a.description FROM public."Description" as a ' +
'INNER JOIN public."product" as b ' +
'ON a.product_code = b.product_code ' +
'WHERE a.product_code = ?',[productInstance.product_code])

Cuase 而不是返回 description : [description] ,它返回 description:[description_fielddb : description]

enter image description here

现在,在 Controller 中我尝试用以下标准替换

       List result = Description.withCriteria{
product{
eq('product_code', productInstance.product_code)
}
projections{
property('description')
}
}

但产品似乎无法访问: enter image description here

描述.groovy

class Description {

String product_code;
String description;

static belongsTo = [product : Product]
static constraints = {
product_code blank:false, size: 1..15
description blank:false, size: 1..16

}
}

Product.grovy

class Product {

String store
String product_code
int price
String notes


static hasOne = [description: Description]

static constraints = {
product_code blank:false, size: 1..15
price blank:false, scale: 2
store blank:false, size: 1..40
notes blank:true , size: 1..150

}



product_code blank:false, size: 1..15
price blank:false, scale: 2
store blank:false, size: 1..40
notes blank:true , size: 1..150

}

我尝试了 grails clean

grails compile --refresh-dependencies

我试图从套件中删除项目并重新导入

最佳答案

您正在以非 grails 方式创建域和查询。

域必须正确关联

产品的映射不是必需的,因为您的域也称为产品,Grails 将生成表“产品”。另一方面,您必须关联其描述。如果存在双向关系。你必须使用“hasOne”

class Product {

String store
String product_code
int price
String notes

static hasOne = [description: Description]

//or directly as a property. Don't do this if you use belongsTo in the other domain.
//Description description

}

描述属于产品,因此必须关联为“belongsTo”

class Description {

String product_code
String description

static belongsTo = [product: Product]

}

如果你想通过产品代码获取所有的描述,你可以在描述域上创建一个条件,通过产品的属性,并获得描述域的描述属性。只需这样做:

List descriptions = Description.withCriteria{
product{
eq('product_code', productInstance.product_code)
}
projections{
property('description')
}
}

您不需要在 grails 中为该简单查询创建 Sql 查询。

关于postgresql - Grails 如何正确加入 withCriteria,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35458136/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com