Java – how to filter by entity class attribute in Hibernate
•
Java
I use hibernate in Java to map classes to database tables I have a personnel list. Each item has many pets and each pet has many toys
I hope to filter these contents in my Dao according to the attributes of toys; For example, find all pets with red toys as list < person > How do I filter?
Personnel type:
@Entity
public class Person {
...
@OneToMany(mappedBy = "person")
private List<Pet> pets;
...
}
Pets:
@Entity
public class Pet {
...
@OneToMany(mappedBy = "pet")
private List<Toy> toys;
...
}
Toys:
@Entity
public class Toy {
...
private String colour;
...
}
Solution
In this case, I'm not sure you mean 'filter', but you can always use HQL
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'
Maybe the condition 'p.pets Size() > 0 'is redundant here due to internal connection
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
