使用分页无法正确排序数据:Sql server和Hibernate 3.6.10

时间:2016-11-13 19:24:46

标签: java hibernate sql-server-2008

编辑问题与已翻译查询的选择部分中是否存在子查询有关。这似乎与给出的编号混乱 分页。

我们最近在hibernate配置中将方言从SqlServerDialect更改为SqlServer2008Dialect,并且休眠它开始翻译使用此sql server查询分页的查询:

   WITH query AS (select ROW_NUMBER() OVER (order by this_.numero asc) as __hibernate_row_nr__, 
                    this_.id as id834_0_, this_.numero as numero834_0_, this_.ragione_sociale as ragione9_834_0_, this_.anagrafica_fiscale as anagrafica10_834_0_, 
                    this_.partita_iva as partita11_834_0_, this_.codice_fiscale as codice12_834_0_, this_.note as note834_0_, this_.data_prospect_dal as data14_834_0_, 
                    this_.dt_inserimento as dt15_834_0_, this_.dt_modifica as dt16_834_0_, this_.nome as nome834_0_, this_.cognome as cognome834_0_, 
                    this_.dt_nascita as dt19_834_0_, this_.fl_piva_duplicata as fl20_834_0_, this_.mail_cliente as mail21_834_0_, this_.fl_capogruppo as fl22_834_0_, 
                    this_.fk_e2_crm_prospects as fk26_834_0_, this_.fl_duplicato as fl23_834_0_, this_.fl_a_consumo as fl4_834_0_, 
                    this_.codice_importazione as codice24_834_0_, this_.codice_attivita as codice38_834_0_, this_.fk_e0_utente_inserimento as fk5_834_0_, 
                    this_.fk_e0_utente_modifica as fk6_834_0_, this_.fk_e0_prof_nodo as fk7_834_0_, this_.fk_e0_decod_classe_dim_pr as fk27_834_0_, 
                    this_.fk_e0_decod_tipo_prospect as fk28_834_0_, this_.fk_e0_decod_codice_ateco as fk29_834_0_, this_.fk_comune_di_nascita as fk30_834_0_, 
                    this_.fk_e0_decod_gp_ind_com as fk31_834_0_, this_.fk_e0_decod_seg_com as fk32_834_0_, this_.fk_e0_decod_sotto_seg_com as fk33_834_0_, 
                    this_.fk_e0_natura_cliente as fk34_834_0_, this_.fk_e0_nazione_nascita as fk36_834_0_, this_.fk_e0_decod_prov_pros as fk35_834_0_, 
                    this_.fk_e2_com_punto_stradario as fk37_834_0_, replace(replace(replace(this_.ragione_sociale,'.',''),'-',''),' ','') as formula1662_0_, 
                    ( select u.user_logged from users u                  join e2_crm_dett_prospects p on u.id = p.fk_e0_decod_gestore_prospect                   
                        where p.fk_e2_crm_prospect = this_.id                    and p.fk_e0_prof_nodo = this_.fk_e0_prof_nodo) as formula1663_0_, 
                    ( select s.descrizione from e0_decod_tipi_stato_prospect s                join e2_crm_dett_prospects p on s.id = p.fk_e0_decod_stato_prospect
                        where p.fk_e2_crm_prospect = this_.id                and p.fk_e0_prof_nodo = this_.fk_e0_prof_nodo) as formula1664_0_, 
                    ( select i.num_dipendenti from e2_crm_info_camerali_prosp i              where i.fk_e2_crm_prospect = this_.id               
                        and i.dt_inserimento = ( select max(p.dt_inserimento)                     
                                                    from e2_crm_info_camerali_prosp p                        
                                                    where p.fk_e2_crm_prospect = this_.id )              ) as formula1665_0_ 

from e2_crm_prospects this_ where this_.fl_duplicato=? ) 

SELECT * FROM query WHERE __hibernate_row_nr__ BETWEEN ? AND ?

问题是numero列未对结果数据进行排序。

使用旧方言,分页查询被翻译为TOP子句,查询末尾有ORDER BY numero,并且行正确排序。

使用条件构建查询,并按如下顺序排序和分页:

Criteria criteria = session.createCriteria(ProspectRicerca.class,"padre"); 

criteria.add(Restrictions.eq(Constant.PROSPECT_FLAG_DUPLICATO, false));
criteria.addOrder(Order.asc("numero"));

criteria.setFirstResult(pageNumber*numElementForPage);
criteria.setMaxResults(numElementForPage);

lista = criteria.list();

hibernate版本是3.6.10,我们使用Sql Server 2008

这是在sql server中执行查询的结果,因为您可以看到行没有排序(__hibernate_row_nr__列与numero列顺序一致。

enter image description here

0 个答案:

没有答案