微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

postgresql – Grails如何正确加入标准

我在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]

enter image description here

现在,在控制器中我试图用以下标准替换

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

但产品似乎无法访问:

enter image description here

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')
  }
}

您不需要在grails中为该简单查询创建SQL查询.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐