@Entity
public class Person{
private String firstName;
private String lastName;
@OneToMany(mappedBy="person" , fetch= FetchType.LAZY)
private Set<Certificate> certificates;
}
@Entity
public class Certificate{
private String courseName;
@ManyToOne
@JoinColumn(name="person_id")
private Person person;
}
拥有实体class(Person)
,其中包含使用@OneToMany
映射的相关实体(证书)的集合(集合),如上所述。
现在我必须抓取all Persons having certificate.courseName IN ("OCPJP", "OCPWCD").
在Simple SQL中,我的查询将是这样的。
select distinct p.firstName, p.lastName from Person p join Certificate c on p.Id = c.person_Id
where c.courseName IN ("OCPJP", "OCPWCD")
如何在JPQL中获得类似的输出?
答案 0 :(得分:0)
这样的查询可以写成如下:
SELECT DISTINCT(c.person) FROM Certificate c WHERE c.courseName IN (:coursenames)
完整示例:
List<String> courseNames = Arrays.asList("OCPJP", "OCPWCD");
List<Person> result = em.createQuery(
"SELECT DISTINCT(c.person) " +
"FROM Certificate c " +
"WHERE c.courseName IN (:coursenames)", Person.class)
.setParameter("coursenames", courseNames)
.getResultList();