加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

postgresql – Grails如何正确加入标准

发布时间:2020-12-13 15:55:15 所属栏目:百科 来源:网络整理
导读:我在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
我在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查询.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读