问题描述
我在使用Oracle的Spring JPA时遇到麻烦。
首先,我需要制作一个元素列表(可分页),但是我只有一个视图可以做所有事情。
这就是我需要在sql语句上使用“ distinct”子句的原因。客户别给我其他方式来获取数据。
课程:
@Entity
@IdClass(TRN04MunicipalityIdEntity.class)
@Table(name = "VISTA_EVENTOS_OGP")
@NamedQuery(name = "TRN04MunicipalityEntity.findId",query = "select distinct e from TRN04MunicipalityEntity e where e.idMunicipio = ?1 and e.idProvincia = ?2"
+ " order by COD_PROV_norA")
@NamedQuery(name = "TRN04MunicipalityEntity.findAllOf",query = "select distinct e from TRN04MunicipalityEntity e where COD_MUN_norA is not null order by COD_PROV_norA")
public class TRN04MunicipalityEntity {
@Id
@Column(name = "COD_MUN_norA",precision = 3,scale = 0)
private BigDecimal idMunicipio;
@Id
@Column(name = "COD_PROV_norA",scale = 0)
private BigDecimal idProvincia;
@Column(name = "MUNICIPIO_ES",length = 50,insertable = false,updatable = false)
@Basic()
private String municipioEs;
@Column(name = "MUNICIPIO_EU",updatable = false)
@Basic()
private String municipioEu;
}
当我在“ findAllOf”上使用分页时,会有一些奇怪的东西。
select
*
from
( select
distinct trn04munic0_.COD_MUN_norA as cod_mun_nora4_0_,trn04munic0_.COD_PROV_norA as cod_prov_nora5_0_,trn04munic0_.MUNICIPIO_ES as municipio_es6_0_,trn04munic0_.MUNICIPIO_EU as municipio_eu7_0_
from
VISTA_EVENTOS_OGP trn04munic0_
where
COD_MUN_norA is not null
order by
COD_PROV_norA )
where
rownum <= ?
但是...计数查询毫无意义
select
count(distinct trn04munic0_.COD_MUN_norA,trn04munic0_.COD_PROV_norA) as col_0_0_
from
VISTA_EVENTOS_OGP trn04munic0_
where
COD_MUN_norA is not null
在count函数上有两个元素,这是胡说八道
有什么想法吗?
坦克很多
解决方法
使用@Query( ... countQuery="...")
显式指定适当的计数查询。