Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在grails上按枚举排序?_Grails_Gorm - Fatal编程技术网

如何在grails上按枚举排序?

如何在grails上按枚举排序?,grails,gorm,Grails,Gorm,我正在尝试按枚举排序,但我在这方面遇到了一些错误 这是我正在使用的代码: and { invoiceTicketDetail { order('date', 'asc') order('code', 'asc') } } lineType { order('sortOrder', 'asc') } 线型是一个枚举,有一个名为sortOrder的int属性,用于正确排列线型。。 问题是,当我尝试执行此操作时,我得到了以下错误 N

我正在尝试按枚举排序,但我在这方面遇到了一些错误

这是我正在使用的代码:

 and {
      invoiceTicketDetail {
         order('date', 'asc')
         order('code', 'asc')
      }
}
lineType {
   order('sortOrder', 'asc')
}
线型是一个枚举,有一个名为sortOrder的int属性,用于正确排列线型。。 问题是,当我尝试执行此操作时,我得到了以下错误

No signature of method: InvoiceService.lineType() is applicable for argument types: (InvoiceService$_tt__getInvoiceDetailList_closure36_closure57) values: [InvoiceService$_tt__getInvoiceDetailList_closure36_closure57@3c7c8544]
Possible solutions: asType(java.lang.Class), asType(java.lang.Class). Stacktrace follows:
groovy.lang.MissingMethodException: No signature of method: InvoiceService.lineType() is applicable for argument types: (InvoiceService$_tt__getInvoiceDetailList_closure36_closure57) values: [InvoiceService$_tt__getInvoiceDetailList_closure36_closure57@3c7c8544]
Possible solutions: asType(java.lang.Class), asType(java.lang.Class)
    at InvoiceService$_$tt__getInvoiceDetailList_closure36$$EPQTbhPk.doCall(InvoiceService.groovy:477)
InvoiceService是我正在使用的服务,但它不是主要对象。如果我只是删除线型线,它可以正常工作,但我需要在此命令中添加此线型

有没有其他方法可以按复杂对象多列对其进行排序?

您不能按线型排序。排序器,因为GORM/Hibernate按域/实体属性排序。它最终成为一个数据库列,但不能使用sort指定数据库列。lineType是域属性,但lineType.sortOrder不是,它是枚举属性。按线型排序将按枚举的序数值排序。这就是存储在数据库中的内容

按SQLProjection排序 最复杂的提示方式(可能不值得这么做)是为枚举创建一个数组,以便将排序值存储在单独的数据库列中。有了这样一个列,就可以使用SQLProjection动态创建属性并按其排序。这种方法的一个缺点是,您将无法返回根实体的实例,例如SomeDomain.withCriteria中的SomeDomain。相反,您可以返回实例ID:

def ids = DomainClass.withCriteria {
    and {
          invoiceTicketDetail {
             order('date', 'asc')
             order('code', 'asc')
          }
    }

    projections {
        property('id')
        sqlProjection 'the_added_column as sortOrder', 'sortOrder', org.hibernate.type.IntegerType as org.hibernate.type.Type
    }

    sort('sortOrder')
}
添加新的域属性 可以将新属性(例如sortOrder)直接添加到域类

棒极了 您可以在Groovy中在客户端进行排序

DomainClass.withCriteria {
    and {
          invoiceTicketDetail {
             order('date', 'asc')
             order('code', 'asc')
          }
    }
}.toSorted { it.lineType.sortOrder }

Emmanuel Rosa,我不明白第一种方法。。。我试了第三个,但没有成功。。。第二个对我来说是不可能的。。。该模型将出错,因为排序器不是此问题解决方案的一部分。。。看来第一个是最好的方法。。。但是我不明白在您将_added_列添加为排序器的地方应该使用什么。首先,您需要创建一个Hibernate用户类型以对应于线型枚举。然后,在Grails中,您可以将表列映射到线型:您已经拥有的用于序号的线型,以及用于排序顺序的新线型。然后可以在sqlprojection ex中使用新列。添加了列。基本上,sqlprojection会从添加的列中动态创建一个属性,以便您可以对其进行排序。