digiarnie
digiarnie

Reputation: 23435

HQL: Finding object where child objects include specific attribute values

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.

Upvotes: 2

Views: 1228

Answers (1)

MJB
MJB

Reputation: 9399

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

Upvotes: 3

Related Questions