Postgresql Grails如何使用Criteria进行正确连接

Postgresql Grails如何使用Criteria进行正确连接,postgresql,grails,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 ' +

我在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\u 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

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

}   
Product.grovy

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

}
}
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的映射不是必需的,因为您的域也被称为product,Grails将生成表“product”。另一方面,你必须把它的描述联系起来。如果存在双向关系。你必须使用“hasOne”

说明属于产品,因此必须与“belongsTo”相关

如果希望通过产品代码获取所有描述,可以通过描述域、产品属性和获取描述域的描述属性创建条件。只要这样做:

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

对于这个简单的查询,不需要在grails中创建Sql查询

你能给我看一下域类代码和产品描述吗?您使用的是哪个版本的grails?您能提供您正在做的事情的更大的图片吗?我觉得奇怪的是,您正在使用Grails,但却忽略了它最好的特性之一:GORM/Hibernate。你的领域模型没有意义。我怀疑您正在使用一个现有的(aka.legacy)数据库,这很好,但需要仔细的域类映射才能使其工作。例如,如果
product\u code
是产品表的主键,那么GORM需要知道这一点;默认情况下,主键是
id
。我是grails的新手,你能告诉我为什么没有意义吗?我更新我的问题可能更有用我想我应该从以下开始:为什么描述在单独的域类中?我看不出有什么好处。不要担心ide无法识别产品。这样做是因为方法产品不存在;当条件查询闭包运行时,它是动态处理的。正如您所建议的,我只在类描述中添加了。。。static belongsTo=[Product:Product]…但是我得到了>Groovy:在静态范围中找到了明显的变量'Product',但没有引用局部变量、静态字段或类。可能原因:我现在很忙,但我可以稍后尝试重现错误。我可以问你为什么从2.x版本开始使用Grails吗?我们正在使用3.x one