0

I got two domains on my project, first is Applicant and other is Screening.

On Applicant I got:

  • String name
  • String age

On Screening I got:

  • String status
  • belongsTo: [applicant: Applicant]

Now I would like to get all applicants with age <30 and status is hired

Here my code:

def applicant = Applicant.queryForApp(params.age)

queryForApp{age->
lt('age', age)
}

However that applicants must have status is hired. I don't know how to filter that applicant with status is "Hired" because, it's on another domain class.

Any solutions will be appreciated.

saw303
  • 8,051
  • 7
  • 50
  • 90
Tran Tam
  • 699
  • 3
  • 14
  • 27
  • Currently not possible since the relationship between `Applicant` and `Screening` is unidirectional. There is no way to query this using criteras unless you add `Screening` to the `Applicant` – saw303 Aug 20 '14 at 05:18

1 Answers1

3

The following criteria should work (not tested):

List<Applicant> applicants = Screening.withCriteria {
    eq 'status', 'HIRED'
    applicant {
        lt 'age', 30
    }
    projections {
        property 'applicant'
    }
}
Iván López
  • 944
  • 4
  • 13
  • 500 thanks for this answer :), where do you learn it from, bro? I checked grails documents, but not much example. – Tran Tam Aug 20 '14 at 09:10
  • I'm glad it worked! I've learned after doing a lot of projects with Grails :-P. In the documentation http://grails.org/doc/latest/guide/GORM.html#criteria, section "Querying with Projections", there's a link to the Hibernate Projections documentation. – Iván López Aug 20 '14 at 12:52