如何从Hibernate规范中的子查询中选择max(id)?

时间:2018-05-29 14:17:00

标签: hibernate hibernate-criteria

我有以下代码生成正确的查询,除了sql中缺少一个表名。

public List<Correspondence> findThreads(String userId, int page, int resultsPerPage) {
    Specification<Correspondence> spec = Specifications.<Correspondence>where((cor, query, cb) -> {
        Root<UserRegisterPerson> ur = query.from(UserRegisterPerson.class);
        Root<Correspondence> cor2 = query.from(Correspondence.class);
        return cb.and(
                cb.equal(ur.get("registerPersonNr"), cor.get("registerPersonNr")),
                cb.equal(ur.get("userId"), userId),
                cb.equal(cor.get("id"),
              // problem:
                query.subquery(Number.class).select(cb.max(cor2.get("id"))).where(cb.and(
                        cb.equal(cor2.get("applicationNumber"), cor.get("applicationNumber")),
                        cb.equal(cor2.get("dossierType"), cor.get("dossierType")),
                        cb.equal(cor2.get("registerPersonNr"), cor.get("registerPersonNr"))))));
    });
    return this.correspondenceRepository.findAll(spec, new PageRequest(page, resultsPerPage)).getContent();
}

使用缺少的表名生成SQL

select generatedAlias0
from 
    nl.boip.correspondencemodule.commons.database.entities.Correspondence as generatedAlias0, 
    nl.boip.correspondencemodule.commons.database.entities.UserRegisterPerson as generatedAlias1, 
    # the next line should actually be further below, where the table name is missing.
    nl.boip.correspondencemodule.commons.database.entities.Correspondence as generatedAlias2
where
    ( generatedAlias1.registerPersonNr=generatedAlias0.registerPersonNr ) 
    and ( generatedAlias1.userId=:param0 )
    and ( generatedAlias0.id=(
        select max(generatedAlias2.id)
        # See next line, the table name is missing
        from  where (
            generatedAlias2.applicationNumber=generatedAlias0.applicationNumber ) 
            and (generatedAlias2.dossierType=generatedAlias0.dossierType )
            and ( generatedAlias2.registerPersonNr=generatedAlias0.registerPersonNr )) )

我的实际案例更复杂,所以我不能将CrudRepository用于此查询(因为sort字段实际上是我的rest-call中的参数)

1 个答案:

答案 0 :(得分:2)

我认为您必须在子查询对象上指定.from,如下所示:

public List<Correspondence> findThreads(String userId, int page, int resultsPerPage) {
    Specification<Correspondence> spec = Specifications.<Correspondence>where((cor, query, cb) -> {
        Root<UserRegisterPerson> ur = query.from(UserRegisterPerson.class);
        Subquery subQuery = query.subquery(Number.class);
        Root<Correspondence> cor2 = subQuery.from(Correspondence.class);
        return cb.and(
                cb.equal(ur.get("registerPersonNr"), cor.get("registerPersonNr")),
                cb.equal(ur.get("userId"), userId),
                cb.equal(cor.get("id"),
              // problem:
                subQuery.select(cb.max(cor2.get("id"))).where(cb.and(
                        cb.equal(cor2.get("applicationNumber"), cor.get("applicationNumber")),
                        cb.equal(cor2.get("dossierType"), cor.get("dossierType")),
                        cb.equal(cor2.get("registerPersonNr"), cor.get("registerPersonNr"))))));
    });
    return this.correspondenceRepository.findAll(spec, new PageRequest(page, resultsPerPage)).getContent();