【发布时间】:2015-05-28 16:06:53
【问题描述】:
在 hasMany 上使用 GORM 默认排序时遇到一个奇怪的错误。我经常在一对多中使用默认排序,从来没有遇到过问题。
请注意,当我删除两个类中的映射子句时,代码运行良好。但我真的希望在数据库中进行默认排序。我知道我也可以用 .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'
【问题讨论】:
-
我个人从未见过像这样的多方面的
sort 'id'映射。我只在“拥有”方面使用过它。如果从 Requirement 中删除排序,它会起作用吗? -
是的。当我从需求中删除排序时,我得到了同样的错误。我还尝试按“名称”位排序,这是相同的错误,但出现错误消息未知列“requiremen0_.name”。
标签: mysql grails many-to-many grails-orm