Criteria API获取集合大小

时间:2012-11-24 11:54:30

标签: java-ee jpa criteria-api

我有两个实体: 第一人(表人);

@Entity
public class Person implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Integer id;
    @Column(name = "name", nullable = false, length = 2147483647)
    private String name;
    @Column(name = "first_name", nullable = false, length = 2147483647)
    private String firstName;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "peopleId")
    private List<PeopleEmail> peopleEmailList;

    //... constuctors
    //... getters setters
}

并且类PeopleEmail

@Entity
public class PeopleEmail implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Integer id;
    @NotNull
    @Column(name = "email", nullable = false, length = 2147483647)
    private String email;
    @JoinColumn(name = "people_id", referencedColumnName = "id", nullable = false)
    @ManyToOne(optional = false)
    private Person peopleId;

    //... constuctors
    //... getters setters
}

正如您所看到的,两个实体都处于一对多的关系中。 我想创建另一个类:

public class PersonAndCompany{
   private String personName;
   private String companyName;
   private int emailCount;

    //... constuctors
    //... getters setters
}

我想写一个填充PersonAndCompany.class字段的typequery,其中包含人名和companyName(另一个类)和电子邮件数,其中人员电子邮件的数量超过2.我想使用标准api。我写了一些代码,但我不知道如何在PersonAndCompany.class中添加条件where和fill emailcount。

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<PersonAndCompany> cq = cb.createQuery(PersonAndCompany.class);
Root<Person> person = cq.from(Person.class);
Join<Person, Company> company = person.join(Person_.companyId);
cq.where(cb.greaterThan(cb.size(person.get(Person_.peopleEmailList)), 2));

Selection<PersonAndCompany> select = cb.construct(PersonAndCompany.class,
                        person.get(Person_.firstName),
                        company.get(Company_.name));
cq.select(select);
TypedQuery<PersonAndCompany> query = em.createQuery(cq);
return query.getResultList();

2 个答案:

答案 0 :(得分:1)

我认为可以在select子句中为一些JPA提供和一些数据库包含一个子查询。

Select p.firstName, c.name, (select count(e) from Email e where e.person = p) from Peron p join p.company c where size(p.emails) > 2

否则,您将需要使用计数和某种分组。然后可以将where子句中的大小检查移动到having子句中。

在JPQL中,它就像是,Criteria就是等价的,

Select p.id, p.firstName, c.name, count(e) from Peron p join p.company c join p.peopleEmaiList e group by p.id, p.firstName, c.name having count(e) > 2

您也可以随时读取对象,然后只需在Java中获取集合的size()。您可以在电子邮件和公司上使用联接或批量提取来避免n + 1个查询。

答案 1 :(得分:0)

当我想将派生或计算的值导入不同的结构时,我对Tuple Queries有了更多的运气,如下所示:

CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery<Tuple> cq = cb.createTupleQuery(); 
// add cq.from and joins 
// add cq.where predicates 
cq.select( cb.tuple(   
  person.get(Person_.firstName).alias("person"),  
  company.get(Company_.name).alias("company"),  
  // and maybe ...
  cb.size(person.get(Person_.peopleEmailList)).alias("emailcount") );
// ...
...
TypedQuery<Tuple> typedQuery = getEntityManager().createQuery(cq);
for(Tuple t : typedQuery.getResultList()) {
  PersonAndCompany ret = new PersonAndCompany();
  ret.setPersonName( t.get("person", String.class) );
  ret.setCompanyName( t.get("company", String.class) );
  ret.setEmailCount( t.get("emailcount", Integer.class) );
}
return ret;
希望有所帮助。祝你好运;)