How to filter attribute attributes of Entity class in Hibernate

I use Hibernate in Java to map classes to DB tables. I have a Person table, each entry contains many pets, each of which has many toys.

I would like to be able to filter them in my DAO based on the attributes of the toys; for example, find all people with pets who have red toys like List<Person>. How can I filter this out?

Character class:

@Entity
public class Person {

    ...
    @OneToMany(mappedBy = "person")
    private List<Pet> pets;

    ...
}

Pet class:

@Entity
public class Pet {

    ...
    @OneToMany(mappedBy = "pet")
    private List<Toy> toys;

    ...
}

Toy Class:

@Entity
public class Toy {

    ...
    private String colour;

    ...
}
+4
source share
1 answer

I'm not sure what you mean by "filter" in this case, but you can always use HQL. For instance:

select p from Person p 
              inner join p.pets as pets
              inner join pets.toys as toys
              where p.pets.size() > 0 
              and toys.color = 'red'

, ' p.pets.size() > 0' - .

0

All Articles