Question

I am trying to run a query to find entries based on 3 fields with "or" conditions. This is my query:

def results = Person.createCriteria().list(params) {
        eq "skillset.id", original.skillset.id
        or { eq "primarySkill.id", original.primarySkill.id }
        or { eq "jobRole.id", original.jobRole.id }
        not { eq "id", original.id }
        order "name", "asc"
    }

In the person object, the 3 fields that I care about: skillset, primarySkill, and jobRole, are instances to another object. I want to find any person where any of those fields match, except for the original person. However, the hibernate query appears to be making an "and" query rather than an "or" query. I could type out the SQL myself, but I'd like to learn how to do it the groovy way.

And this is the query generated by hibernate

select this_.id as id5_0_, 
this_.version as version5_0_, 
this_.account_id as account3_5_0_, 
this_.band as band5_0_, t
his_.end_date as end5_5_0_, 
this_.job_role_id as job6_5_0_, 
this_.name as name5_0_, 
this_.primary_skill_id as primary8_5_0_, 
this_.professional_market_place as professi9_5_0_, 
this_.project_delivery_manager_id as project10_5_0_, 
this_.project_manager_id as project11_5_0_, 
this_.project_name as project12_5_0_, 
this_.rate as rate5_0_, 
this_.resource_deployment_manager_id as resource14_5_0_, 
this_.skillset_id as skillset15_5_0_, 
this_.start_date as start16_5_0_, 
this_.work_location as work17_5_0_ 
from person this_ where this_.skillset_id=? 
and (this_.primary_skill_id=?) 
and (this_.job_role_id=?) 
order by this_.name asc limit ?
Was it helpful?

Solution

I believe it should be

def results = Person.createCriteria().list(params) {
    or {
        eq "skillset.id", original.skillset.id
        eq "primarySkill.id", original.primarySkill.id
        eq "jobRole.id", original.jobRole.id
    }
    not { eq "id", original.id }
    order "name", "asc"
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top