2

This is an extension of this question.

Instead of SQL, how would I do the same using HQL? (i.e. find all dogs that have both the colors of black and white but could have other colors)

Let's say my entity classes look like this:

public class Dog {
    private Long id;
    private Set<DogColor> colors;
    private String name;
    private String size;

    ...
}

public class DogColor {
    private Long id;
    private String color;

    ...
}

Then I want my HQL to look a little something like this:

from dog where dog.colors contains ("black", "white")

Obviously this is just pseudo-code since dog.colors is an object and not the color strings.

1 Answer 1

3
Select d from dog d join d.colors o where o.color in ("black","white")

Of course you need to have the right mapping set up

1
  • Thanks! using the child of the parent in the join and using 'where' instead of 'on' is where I went wrong.
    – digiarnie
    May 2, 2011 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.