Sudhir N
Sudhir N

Reputation: 4096

Grails Unidirectional Many to One and Criteria

Is there any way, to query (criteria) list of Many side with restrictions on one side in an Unidirectional many to one relationship

Domain

class Batch {

    String name
    Date date

}


class Record {
    String type
    Batch batch
}

class RecordDetails {
   String xx
   Record record
}

Is there anyway to criteria query all batch records, where batch.date = xx, and record.type = yy and recordDetails.xx = zz

HQL should work, but is there any other way with criteria other then in queries.

Upvotes: 1

Views: 219

Answers (1)

Gregor Petrin
Gregor Petrin

Reputation: 2931

With Where queries and Detached criteria out of the question one option would be using sqlRestriction like so:

Batch.withCriteria {
    sqlRestriction """
        exists (
            select * from record r
            join record_details rd on rd.record_id = r.id 
            where 
                r.batch_id = {alias}.id and 
                r.type like ? and 
                rd.xx like ?
        )""", [ 'foo', 'bar' ]
}

Upvotes: 1

Related Questions