【问题标题】:QueryDSL build a query with subclassQueryDSL 使用子类构建查询
【发布时间】:2017-04-08 08:56:46
【问题描述】:

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

这是我的实体。

@Entity
public class Region {

  @Id
  private Integer id;

}

@Entity
public class RegionTemp extends Region {}

查询者

@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();
    }
}

【问题讨论】:

    标签: java jpql querydsl


    【解决方案1】:

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

    @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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-13
      • 1970-01-01
      • 1970-01-01
      • 2013-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多