如何通过字母数字(String)属性更正排序域类?

时间:2015-02-19 15:49:42

标签: oracle hibernate grails

当我用字母数字属性对Domain Class进行排序时,当值以数字开头时,排序顺序不是预期的。例如:

class MyDomainClass {
  String descr
}

MyDomainClass.createCriteria().list() {
  order("descr", "asc")
}.each { it ->
  println it.descr
}

如果表格保持这3个值:

  • AAA
  • 111
  • BBB

打印结果为:

  • AAA
  • BBB
  • 111

但我想要的是(与数据库查询结果相同):

  • 111
  • AAA
  • BBB

有没有我可以定义的配置?

1 个答案:

答案 0 :(得分:0)

我意识到当我的应用程序将语言环境设置为WEST_EUROPEAN时,由于{DB(3)}将Oracle DB更改为pt_BR而导致问题发生(因为这是此语言环境的默认排序{ {3}})。所以我意识到我可以在必要时使用NLS_SORT parameter函数定义自定义排序器:

select ret
  from ( select 'AAA' as ret from dual
          union
         select '111' as ret from dual
          union
         select 'BBB' as ret from dual)
 ORDER BY NLSSORT(ret, 'NLS_SORT = BINARY');

在grails中:

println ' --- With WEST_EUROPEAN sort ---'
MyDomain.executeQuery("SELECT m FROM MyDomain as m ORDER BY NLSSORT(descr, 'NLS_SORT = WEST_EUROPEAN')").each {
    println it.descr
}

println ' -- With BINARY sort ---'
MyDomain.executeQuery("SELECT m FROM MyDomain as m ORDER BY NLSSORT(descr, 'NLS_SORT = BINARY')").each {
  println it.descr
}

输出:

--- With WEST_EUROPEAN sort ---  
AAA  
BBB  
111  
--- With BINARY sort ---  
111  
AAA  
BBB