我在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_fielddb:description]
现在,在控制器中我试图用以下标准替换
List result = Description.withCriteria{ product{ eq('product_code',productInstance.product_code) } projections{ property('description') } }
Description.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方式创建域和查询.
域名必须正确相关
产品的映射不是必需的,因为您的域名也称为Product,Grails将生成表“product”.另一方面,你必须将其描述联系起来.如果存在双向关系.你必须使用“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') } }