QueryDSL使用子类构建查询

时间:2016-11-24 07:02:17

标签: java jpql querydsl

我正在QueryDSL中构建一个查询。我有实体和子类实体具有相同的列。我想仅使用单个JPAQuery对两个实体使用相同的查询。

这是我的实体。

@Entity
public class Region {

  @Id
  private Integer id;

}

@Entity
public class RegionTemp extends Region {}

queryer

@Component
public class RegionQueryer {

    @PersistenceContext
    private EntityManager mysqlEntityManager;

    QRegion qRegion = QRegion.region; // ???

    public Integer loadLastId() {

        return new JPAQueryFactory(mysqlEntityManager)
                .select(qRegion.id)
                .from(qRegion)
                .orderBy(qRegion.id.desc()).fetchFirst();
    }
}

1 个答案:

答案 0 :(得分:0)

我的代码。这个样本。如果你想使用单一查询。使用简单的存储库。它很容易找到,删除,保存。你搜索JPA Tutorial。

@Override
public List<CompanyInformaion> findCompanyInformationList(String language, Association association) {
    QCompanyInformaion qCompanyInformaion = QCompanyInformaion.companyInformaion;
    QCompany qCompany = QCompany.company;

    EntityManager em = entityManagerFactory.createEntityManager();
    JPAQuery jpaQuery = new JPAQuery(em);

    List<CompanyInformaion> infos = jpaQuery.from(qCompanyInformaion)
            .where(qCompanyInformaion.language.eq(language)
                    .and(qCompanyInformaion.company.in(new JPASubQuery().from(qCompany)
                            .where(qCompany.association.eq(association)).list(qCompany))))
            .orderBy(qCompanyInformaion.companyName.asc()).list(qCompanyInformaion);

    return infos;
}