如何 select 来自 Hibernate 规范中的子查询的 max(id)?
How to select on max(id) from sub-query in Hibernate Specification?
我有以下生成正确查询的代码,除了 sql 中缺少一个 table 名称。
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,缺少 table 名称
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 进行此查询(因为排序字段实际上是我的 rest-call 中的一个参数)
我认为您必须像这样在子查询对象上指定 .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();
我有以下生成正确查询的代码,除了 sql 中缺少一个 table 名称。
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,缺少 table 名称
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 进行此查询(因为排序字段实际上是我的 rest-call 中的一个参数)
我认为您必须像这样在子查询对象上指定 .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();