简体   繁体   English

如何在Hibernate中按实体类的属性属性进行过滤

[英]How to filter by attributes of attributes of an Entity class in Hibernate

I'm using Hibernate in Java to map classes to DB tables. 我在Java中使用Hibernate将类映射到数据库表。 I have a Person table, each entry has many Pets, each of which has many Toys. 我有一个人员表,每个条目有很多宠物,每个宠物都有很多玩具。

I'd like to be able to filter these in my DAO based on attributes of the toys; 我希望能够根据玩具的属性在我的DAO中过滤这些内容; for example, find all people with pets that have red toys, as a List<Person> . 例如,找到所有带有红色玩具的宠物的人,作为List<Person> How can I filter on this? 我该如何过滤?

Person 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;

    ...
}

I am not sure what you mean 'filter' in this case, but you can always use HQL. 在这种情况下,我不确定你的意思是'过滤器',但你总是可以使用HQL。 For example: 例如:

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 where condition ' p.pets.size() > 0 ' is redunant here, because of inner join. 也许条件' p.pets.size()> 0 '在这里是冗余的,因为内连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM