Grails, how to join Criteria

I have the following statement in grails 2.0.3 that works fine The query I want to change by criteria

    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 instead returns a description: [description], it returns a description: [description_fielddb: description]

enter image description here

Now in the controller, I tried to replace the following criteria:

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

but the product does not seem to have access: 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

}   

}

I tried grails cleanand

grails compile --refresh-dependencies

I tried to remove the project from the package and import again

+4
source share
1 answer

You create domains and query in a non-grails way.

Domains must be properly connected

, Product, Grails "". . . "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 

}

, ""

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

Sql grails .

+3

Source: https://habr.com/ru/post/1629188/


All Articles