SELECT投影中的QueryDSL获取集集合

时间:2018-10-24 11:08:09

标签: hibernate jpa spring-data-jpa querydsl

我有一个嵌套的JPA实体结构,如下所示

@Entity
public class Applicant {

  @OneToMany()
  private Set<Address> addresses;

  // other applicant fields

}

@Entity
public class Address {

  private String houseName;

  private String houseNumber;

  private String postcode;

}

我试图通过以下查询获取带有地址集的申请人详细信息,但它返回“类型不匹配”

private static final ConstructorExpression<AddressEntity> qAddresses = QAddressEntity.create(
        qAddressEntity.houseName, qAddressEntity.houseNumber, qAddressEntity.postcode);

@Test
public void shouldTestAddressWithProjection() {
    JPAQuery<ApplicationEntity> jpaQuery = new JPAQuery<>(entityManager);
    QueryResults<ApplicantEntity> queryResults = jpaQuery.select(
            Projections.bean(ApplicantEntity.class, qApplicantEntity.forename, qApplicantEntity.surname,
                    GroupBy.set(qAddresses).as("addresses")))
            .from(qApplicantEntity)
            .leftJoin(qApplicantEntity.addresses, qAddressEntity)
            .where(qApplicantEntity.forename.startsWithIgnoreCase("Nikki"))
            .fetchResults();

}

我知道我可以使用.transform()并且可以使用的事实是,但是我放弃了fetchResults()附带的分页。请让我知道此问题的解决方案。

0 个答案:

没有答案