根据子实体和父实体属性的规范过滤父表和子表

时间:2018-07-27 06:25:06

标签: hibernate jpa spring-data-jpa jpa-2.0 hibernate-criteria

我有一个JPA实体,该实体与子实体具有@OnetoMany关系。我已经根据子代和父代属性编写了类型为parent的规范。它过滤父记录,但获取父记录的所有子记录。正如我在其他线程上所读到的那样,由于规范是父类型的,因此它不会过滤子级。根据一些线程,JPA不提供此功能。我是JPA /休眠的新手,任何人都可以帮助我如何过滤父母和子女。 示例:-

    @Entity
public class Employee implements Serializable
{
   id, name, address ....

 @OneToMany(targetEntity = Children.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumns(value = {})
    private List<Children> childList = new ArrayList<Children>(0);
}

public class Children implements Serializable
{
   id, name, ....
   String sex;

}

Specification<Employee>  employeeSpecification = ....

Specification<Employee> specs = return (root, query, cb) -> {
            query.distinct(true);
            return where(
                    where(employeeSpecification))
                            .and(childrenSpecification("sex", sex))
                            .toPredicate(root, query, cb);
        };


@Override
    public Specification<Employee> getFilter(String attribute, String value) {

return (root, query, cb) -> {
            if (value == null) {
                return null;
            }

            ListJoin<Employee, Children> childList = root.joinList("childList",
                    JoinType.INNER);

            return cb.like(
                    cb.lower(childList.get(attribute)),
                    containsLowerCase(value));
        };
    }

我能够过滤所有有任何女性(或“男性”)孩子的Employee,但是在加载员工时会加载该员工的所有孩子。谁能帮我解决这个问题,任何例子都会有帮助的。

0 个答案:

没有答案
相关问题