0

I already searched a lot but I didn't found a solution yet. I guess I really need your help. My problem is about filtering with a "special" goal. Let's say we have got a few Persons, who have got many skills. I am able to filter this Persons by name and matching skills. But I need to get these persons, who just have one explizit skill and no other. i.e. persons which can run very fast but nothing more than this :-). But it is possible that a person have many entries of the skill swim. i.e. Peter which has got three skills: [swim, swim, swim] should be in the result too. An example is following. I am really looking forward to hear some tips. Thank you very much.

class Person {
    String name;
    static hasMany = [skills: Skill]
}

class Skill {
    Double skillLevel;
    SkillType skillName;
}


class ControllerClass {

def filterMethod() {
    def personCriteria = Person.createCriteria()
    results = personCriteria.list(params) {
        and {
            eq("name", "Peter")


            //I can do this to get Person, who have the skill to swim
            skills {
                eq("skillName", "swim")
            }

            //Problem: this Person might have other skills, too. i.e. "run fast", "jump high", "Math"



            //My Goal is: I want only these persons, who only have the skill "swim" and no other skill, but the item swim can appear more than once 
        }
    }
}

}

In my scenario I know all different types of skills, so they are limited. This would make it easier wouldn't it? Let's pretend there are three skills possible: swim, run, sing. But they can appear more than once. Why does something like this doesn't work?:

and {
    and {
        sizeGe("skills", 1)
        skills{
                eq("skillName", "swim")

        }
    }

    and {
        sizeEq("skills", 0)
        skills {
            eq("skillName", "run")
        }
    }

    and {
        sizeEq("subgruppen", 0)
        skills {
            eq("skillName", "sing")
        }
    }
}
waywayway
  • 53
  • 4

1 Answers1

0

You are on the right path - sizeEq should do what you whant. You just used it the wrong way. As described in the docs, you could check the size of a collection.

Try this:

def results = personCriteria.list(params) {
    and {
        sizeEq("skills", 1)
        skills {
            eq("skillName", "swim")
        }
    }
}

This should get you all person who have exactly one skill with the name 'swim'.

aiolos
  • 4,637
  • 1
  • 23
  • 28
  • this is a really good start, thank you. But I forgot to mention, that a Persons should be in the result which have got many swim entries. This makes no sence in this example but in my project it does. i.e. a person which have swim, swim, swim should be in the result too – waywayway Jan 29 '15 at 09:01
  • I used the sizeEq function the wrong way. Thank you for your advice, so I edited my question above. – waywayway Jan 29 '15 at 10:34