JPA / Hibernate sql语句与另一个集合中的集合有可能吗?

时间:2012-11-21 12:18:20

标签: hibernate

如何编写这个jpa / hibernate sql语句(如果可能的话)

我有2个实体

@Entity
public class Employees    
  @OneToMany(cascade=CascadeType.ALL)
  private List<Benefits> benefits;

=======================================

@Entity    
public class Benefits
  private String department

我需要检索列表中的员工列表 某些部门列表中包含的福利。

我编写了以下JPA / Hibernate sql语句,

from Employees e where e.benefits exists ( from Benefits b where b.department in (?1) )", someListOfDepartments )

但它会引发异常。是否可以进行此类查询?

1 个答案:

答案 0 :(得分:0)

HQL:

from Employees e INNER JOIN e.benefits b where b.department in (:depts)

标准:

session.createCriteria(Employees.class);
    criteria3.createAlias("benefits", "b",
            CriteriaSpecification.INNER_JOIN)
            .setFetchMode("b", FetchMode.JOIN)
    .add(Restrictions.in("b.department", depts));