在hasMany上使用GORM默认排序时,我遇到了一个奇怪的错误。我经常在一对多中使用默认排序,从未遇到过问题。
请注意,当我删除两个类中的mapping子句时,代码运行完美。但我真的想在数据库中进行默认排序。我知道我也可以用.sort {it.id}
解决这个问题这是我的域类:
abstract class Product {
String nr
String name
static hasMany = [requirements:Requirement]
static mapping = {
requirements sort: 'id'
}
class Requirement {
String name
String imageName
static hasMany = [products:Product]
static belongsTo = Product
static constraints = {
name(nullable:false, blank:false)
imageName(nullable:false, blank:false)
}
static mapping = {
sort 'id'
}
}
现在,当我打电话
def product = Product.findByNr("test")
log.debug pr.requirements
我得到了
2015-05-28 17:58:03,374 [http-bio-8080-exec-6] ERROR spi.SqlExceptionHelper - Unknown column 'requiremen0_.id' in 'order clause'
Error |
2015-05-28 17:58:03,455 [http-bio-8080-exec-6] ERROR errors.GrailsExceptionResolver - MySQLSyntaxErrorException occurred when processing request: [GET] /testDb
Unknown column 'requiremen0_.id' in 'order clause'. Stacktrace follows:
Message: Unknown column 'requiremen0_.id' in 'order clause'
答案 0 :(得分:0)
似乎排序应该应用于当前类的字段,而不是关联类的字段:https://grails.github.io/grails-doc/latest/ref/Database%20Mapping/sort.html **
将它排序为对当前查询类进行排序的想法:
自定义默认属性以对查询结果进行排序。 **
例如,您可以按名称对产品进行排序,但如何按照一组相关要求的ID对产品进行排序?恕我直言,它没有多大意义。
尝试从产品中删除要求排序:'id'。
BTW你有抽象产品的具体子类吗?