如何在应用过滤器查询

时间:2015-09-07 10:48:39

标签: querydsl

我有两个实体

@Entity
@Table(name = "employee")
public class Employee {

............
@ManyToMany(mappedBy = "employees")
private ArrayList<Department> departments = new ArrayList<>();

...getter..setters' 

@Entity
@Table(name = "department")
 {
...............
@ManyToMany
@JoinTable(
        name = "m_employee_department",
        joinColumns = @JoinColumn(name = "department_id",     referencedColumnName = "department_id"),
        inverseJoinColumns = @JoinColumn(name = "employee_id", referencedColumnName = "employee_id"))
private ArrayList<Employee> employees;

我在JPA上使用QueryDsl。在UI中,我想搜索所有具有过滤器作为部门的员工。员工和部门的关系中有很多人。出于这个原因,我想在我的QueryDsl查询中应用过滤器

之类的东西
QDepartment department = QDepartment.department;
QEmployee employee = QEmployee.employee;
if (departments != null && departments.size() > 0) {
            BooleanExpression booleanExpression = null;
            for (int count = 1; count < categories.size(); count++) {
                booleanExpression = booleanExpression.or(
                        employee.departments._____(department.get(count)));
            }
            builder.and(booleanExpression);
        }

但问题是employee.department返回SimplePath&gt;我想迭代这个,以便我可以将employee.departments id与department.id匹配(从UI获取)

1 个答案:

答案 0 :(得分:0)

这样的事情应该有效

QEmployee employee = QEmployee.employee;
BooleanBuilder builder = new BooleanBuilder();
if (departments != null) {
    for (Department dep : departments) {
        builder.or(employee.departments.any().eq(dep)); 
    }
}

query.where(builder)...