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查询.

相关文章

项目需要,有个数据需要导入,拿到手一开始以为是mysql,结果...
本文小编为大家详细介绍“怎么查看PostgreSQL数据库中所有表...
错误现象问题原因这是在远程连接时pg_hba.conf文件没有配置正...
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本...
wamp 环境 这个提示就是说你的版本低于10了。 先打印ph...
psycopg2.OperationalError: SSL SYSCALL error: EOF detect...