来自集合的bean字段的JPA查询

时间:2013-09-30 14:30:47

标签: java sql jpa

我有下一堂课关系:

public class Company {
  . . .
  @OneToMany(mappedBy = "company", fetch = FetchType.EAGER, cascade=CascadeType.PERSIST)
  private Set<CompanySecUser> companySecUsers;
  . . .
}
public class CompanySecUser{
    . . .
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="USER_ID")
    private User secUser;

    @Column(name = "IS_READ")
    private BigDecimal isRead;
    . . .
}

现在如何在公司实体的JPA中编写查询,只获得指定secUser.id和isRead = 1的公司?

2 个答案:

答案 0 :(得分:1)

List<Company> list = session.createQuery("from Company company where company.companySecUsers.isRead=:isRead").setParameter("isRead",1).list();

答案 1 :(得分:1)

select company from Company company 
    join company.companySecUsers user 
    where user.id = <ID> and user.isRead = 1