如何在不使用JOIN的情况下为JPQL中的Collection子句执行参数绑定?

时间:2014-12-12 20:03:16

标签: java mysql hibernate jpa jpql

以下是数据建模方案:

为了便于说明,请考虑这个简单的数据模型:

ManyToMany从项目到部门

ManyToMany从项目到员工

考虑这个权利:

@Entity
@NamedQueries( {
@NamedQuery(name = "project.getProjectByDepartmentAndEmployee", 
query = "SELECT p from Project p WHERE (:dept) in p.department and (:emp) in p.employees"),
})
public class ConfigFile extends BaseUpdatableEntity implements Serializable {

    private Long id;

    @ManyToMany
    private List<Department> departments;


    @ManyToMany
    private List<Employee> employees;

}

在My Dao Layer中,我想执行以下操作:

public Project getProject(Department dept, Employee emp){
       Query query = getEntityManager().createNamedQuery("project.getProjectByDepartmentAndEmployee");
        query.setParameter("dept", dept)
             .setParameter("emp",emp);
            return (UserSession) query.getResultList().get(0);
}

我在上面的NamedQueryConstruction中看到的问题。 如何在左侧的Where子句中绑定参数。

感谢您的帮助

注意

以下答案将有效。但我正在寻找一种解决方案,如果可能的话不使用JOIN

1 个答案:

答案 0 :(得分:1)

尝试将您的JPQL查询更改为:

SELECT p 
from Project p 
inner join p.department d
inner join p.employees e
WHERE d = :dept and e = :emp