使用 eachWithIndex 引导域

bootstrapping domains with eachWithIndex

另一个令人头疼的问题 :-( 请有人帮忙。我只是想在我的 Grails 3 项目的 bootstrap 文件中使用 eachWithIndex 批量创建域实例。

这是我的域名class...

package ttt_server

class TttPriority {

    String name
    int order
    Date dateCreated

    static constraints = {
        name blank: false, nullable: false
        order blank: false, nullable: false
        dateCreated nullable:true, blank:true
    }

}

这是我的 bootstrap 批量创建代码...

["Mortal","Major","Critical","Minor","Nice To Have"].eachWithIndex{ name, idx ->
    new TttPriority(name: name, order: idx).save(flush:true)
}

我快要在这个问题上拔头发了。

这是错误....

2017-07-31 16:30:41.755 ERROR --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order) values (0, 'Mortal', 0)' at line 1 2017-07-31 16:30:41.817 ERROR --- [ main] o.s.boot.SpringApplication >: Application startup failed

org.springframework.jdbc.BadSqlGrammarException: Hibernate operation: could not execute statement; bad SQL grammar [n/a]; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order) values (0, 'Mortal', 0)' at line 1 at ...

order 是一个 SQL 关键字。您需要重命名或重新映射 属性.

static mapping = {
    order column: "my_order"
}

因为 order 是一个保留字,您必须为您的列使用其他名称。或者用反引号

包围您的列名称

示例

static mapping = {
    order column: "order_col"
}

static mapping = {
    order column: "`order`"
}