我可以在2.4.4中创建一个简单的grails应用程序,但是只要我添加了静态映射,它就会改变主键的名称而拒绝保存。这是我使用的域类。
class Liaison {
String first;
String last;
static constraints = {}
static mapping = {
id column:'liaisonId', type: 'integer'
}
}
这是生成的控制器
import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional
@Transactional(readOnly = true)
class LiaisonController {
static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
def index(Integer max) {
params.max = Math.min(max ?: 10, 100)
respond Liaison.list(params), model:[liaisonInstanceCount: Liaison.count()]
}
def show(Liaison liaisonInstance) {
respond liaisonInstance
}
def create() {
respond new Liaison(params)
}
@Transactional
def save(Liaison liaisonInstance) {
if (liaisonInstance == null) {
notFound()
return
}
if (liaisonInstance.hasErrors()) {
respond liaisonInstance.errors, view:'create'
return
}
liaisonInstance.save flush:true
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.created.message', args: [message(code: 'liaison.label', default: 'Liaison'), liaisonInstance.id])
redirect liaisonInstance
}
'*' { respond liaisonInstance, [status: CREATED] }
}
}
def edit(Liaison liaisonInstance) {
respond liaisonInstance
}
@Transactional
def update(Liaison liaisonInstance) {
if (liaisonInstance == null) {
notFound()
return
}
if (liaisonInstance.hasErrors()) {
respond liaisonInstance.errors, view:'edit'
return
}
liaisonInstance.save flush:true
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.updated.message', args: [message(code: 'Liaison.label', default: 'Liaison'), liaisonInstance.id])
redirect liaisonInstance
}
'*'{ respond liaisonInstance, [status: OK] }
}
}
@Transactional
def delete(Liaison liaisonInstance) {
if (liaisonInstance == null) {
notFound()
return
}
liaisonInstance.delete flush:true
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.deleted.message', args: [message(code: 'Liaison.label', default: 'Liaison'), liaisonInstance.id])
redirect action:"index", method:"GET"
}
'*'{ render status: NO_CONTENT }
}
}
protected void notFound() {
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'liaison.label', default: 'Liaison'), params.id])
redirect action: "index", method: "GET"
}
'*'{ render status: NOT_FOUND }
}
}
}
我在liaisonInstance.save中收到错误:在def save()中为true,但我不明白为什么。如果我查看我的更改日志,表格看起来没问题。它是某种隐藏的哈希映射吗?任何帮助都会很棒。
答案 0 :(得分:1)
哦,我想我明白了。我过分思考。静态映射应该是
static mapping = {
id column: 'liaisonId'
}
而不是
static mapping = {
id column: 'liaisonId', type: 'integer'
}
我认为它与自动生成的主键有关,是一个bigInt。我认为类型仅适用于您计划使用id生成器作为主键。