【问题标题】:Provide a default secondary sort column in grails 3.3.9在 grails 3.3.9 中提供默认的辅助排序列
【发布时间】:2019-12-31 00:56:36
【问题描述】:

我有一个问题,当使用createCriteria().list(params) 以每页 10 条记录的方式翻阅一组 12 条记录时,第 2 页上显示的 2 条记录出现在第 1 页上。在域中指定了默认“排序”映射,但指定的列包含重复项,因此我假设排序列中具有重复项的子集的顺序是不确定的。

这也使用可排序的列标题,就像 grails 在您生成全部时一样。这是否意味着任何时候选定的排序列包含重复的顺序可能是不确定的?有没有办法(除了蛮力)传递一个默认的辅助排序列(可能是 ID),除了选择的列来保证顺序吗?

这是在 SQL Server 上,这些是生成的查询:

第 1 页:

select TOP(?) 
    this_.procedure_occurrence_id as procedur1_22_1_, 
    this_.provenance_id as provenan2_22_1_, 
    this_.procedure_date as procedur3_22_1_, 
    this_.procedure_type_concept_id as procedur4_22_1_, 
    this_.procedure_source_value as procedur5_22_1_, 
    this_.associated_provider_id as associat6_22_1_, 
    this_.relevant_condition_concept_id as relevant7_22_1_, 
    this_.visit_occurrence_id as visit_oc8_22_1_, 
    this_.person_id as person_i9_22_1_, 
    this_.procedure_concept_id as procedu10_22_1_, 
    person_ali1_.person_id as person_i1_20_0_, 
    person_ali1_.person_source_value as person_s2_20_0_, 
    person_ali1_.provenance_id as provenan3_20_0_, 
    person_ali1_.location_id as location4_20_0_, 
    person_ali1_.gender_concept_id as gender_c5_20_0_, 
    person_ali1_.care_site_id as care_sit6_20_0_, 
    person_ali1_.year_of_birth as year_of_7_20_0_, 
    person_ali1_.provider_id as provider8_20_0_, 
    person_ali1_.race_concept_id as race_con9_20_0_, 
    person_ali1_.month_of_birth as month_o10_20_0_, 
    person_ali1_.ethnicity_concept_id as ethnici11_20_0_, 
    person_ali1_.ethnicity_source_value as ethnici12_20_0_, 
    person_ali1_.race_source_value as race_so13_20_0_, 
    person_ali1_.gender_source_value as gender_14_20_0_, 
    person_ali1_.day_of_birth as day_of_15_20_0_ 
from procedure_occurrence this_ 
inner join person person_ali1_ on this_.person_id=person_ali1_.person_id 
where (person_ali1_.person_id=?) 
and this_.provenance_id=? 
and this_.procedure_concept_id in (?, ?, ?, ?) 
order by this_.procedure_date desc

第 2 页:

WITH query AS (
    SELECT inner_query.*, ROW_NUMBER() 
    OVER (ORDER BY CURRENT_TIMESTAMP) as __hibernate_row_nr__ 
    FROM ( 
        select TOP(?) this_.procedure_occurrence_id as procedur1_22_1_, 
            this_.provenance_id as provenan2_22_1_, 
            this_.procedure_date as procedur3_22_1_, 
            this_.procedure_type_concept_id as procedur4_22_1_, 
            this_.procedure_source_value as procedur5_22_1_, 
            this_.associated_provider_id as associat6_22_1_, 
            this_.relevant_condition_concept_id as relevant7_22_1_, 
            this_.visit_occurrence_id as visit_oc8_22_1_, 
            this_.person_id as person_i9_22_1_, 
            this_.procedure_concept_id as procedu10_22_1_, 
            person_ali1_.person_id as person_i1_20_0_, 
            person_ali1_.person_source_value as person_s2_20_0_, 
            person_ali1_.provenance_id as provenan3_20_0_, 
            person_ali1_.location_id as location4_20_0_, 
            person_ali1_.gender_concept_id as gender_c5_20_0_, 
            person_ali1_.care_site_id as care_sit6_20_0_, 
            person_ali1_.year_of_birth as year_of_7_20_0_, 
            person_ali1_.provider_id as provider8_20_0_, 
            person_ali1_.race_concept_id as race_con9_20_0_, 
            person_ali1_.month_of_birth as month_o10_20_0_, 
            person_ali1_.ethnicity_concept_id as ethnici11_20_0_, 
            person_ali1_.ethnicity_source_value as ethnici12_20_0_, 
            person_ali1_.race_source_value as race_so13_20_0_, 
            person_ali1_.gender_source_value as gender_14_20_0_, 
            person_ali1_.day_of_birth as day_of_15_20_0_ 
        from procedure_occurrence this_ 
        inner join person person_ali1_ on this_.person_id=person_ali1_.person_id 
        where (person_ali1_.person_id=?) 
        and this_.provenance_id=? 
        and this_.procedure_concept_id in (?, ?, ?, ?) 
        order by this_.procedure_date desc ) inner_query ) 
    SELECT 
        procedur1_22_1_, 
        provenan2_22_1_, 
        procedur3_22_1_, 
        procedur4_22_1_, 
        procedur5_22_1_, 
        associat6_22_1_, 
        relevant7_22_1_, 
        visit_oc8_22_1_, 
        person_i9_22_1_, 
        procedu10_22_1_, 
        person_i1_20_0_, 
        person_s2_20_0_, 
        provenan3_20_0_, 
        location4_20_0_, 
        gender_c5_20_0_, 
        care_sit6_20_0_, 
        year_of_7_20_0_, 
        provider8_20_0_, 
        race_con9_20_0_, 
        month_o10_20_0_, 
        ethnici11_20_0_, 
        ethnici12_20_0_, 
        race_so13_20_0_, 
        gender_14_20_0_, 
        day_of_15_20_0_ 
    FROM query 
    WHERE __hibernate_row_nr__ >= ? 
    AND __hibernate_row_nr__ < ?

【问题讨论】:

    标签: sql-server hibernate grails


    【解决方案1】:

    我认为可以在域类中定义多个排序 cloumn:

        static mapping = { 
              sort(["property1" : "asc",
                    "property2" : "desc"] ) 
        } 
    

    【讨论】:

    • 是的,这适用于默认排序。谢谢! But I need to do something similar when the header is selected and passed through params.有没有办法将多个属性传递给
    • 您可以使用formula 构建一个额外的“排序属性”。例如:class SomeDomain { String category String subcategory String sortProperty static mapping = { sortProperty formula: 'category || subcategory' } }我不知道,是否符合您的需求,(这是一个非常 SQL 的解决方案)
    • 谢谢!这是我不会想到的,但在这一点上,我认为我宁愿在标准范围内管理它。
    • 嗯,如果我可以在标准中管理它...似乎如果我将参数中的排序和顺序传递给 createCriteria().list(params),它会覆盖域中的默认排序并一切都很好,但是如果我只是传递 max 和 offset 并尝试在标准正文中添加一个 order 语句,它也会应用默认排序,我得到一个错误:在 order by 列表中多次指定了一个列。 order by 列表中的列必须是唯一的。
    • 我将此标记为已接受,因为它确实回答了我的问题,尽管我最终在标准中进行了排序。需要注意的是:我可以在域上设置一个默认的按 id 排序,它可以作为辅助排序列,只要它们不一样,就会在参数中传递主列,这会导致我提到的错误更多。不确定这是否是故意行为,但很有趣......
    猜你喜欢
    • 1970-01-01
    • 2014-05-24
    • 1970-01-01
    • 2011-04-12
    • 2010-10-05
    • 2023-03-19
    • 2011-05-09
    • 2017-10-25
    • 1970-01-01
    相关资源
    最近更新 更多