我自己在 GORM 中的 ID [英] my own id in GORM
问题描述
我尝试更改 grails 中的标准id":
I tried to change the standard 'id' in grails:
calls Book {
String id
String title
static mapping {
id generator:'assigned'
}
}
不幸的是,我很快注意到这破坏了我的引导程序.而不是
unfortunately, I soon noticed that this breaks my bootstrap. Instead of
new Book (id:'some ISBN', title:'great book').save(flush:true, failOnError:true)
我不得不使用
def b = new Book(title:'great book')
b.id = 'some ISBN'
b.save(flush:true, failOnError:true)
否则我会收到必须在调用 save() 之前手动分配此类的 id"错误.
otherwise I get an 'ids for this class must be manually assigned before calling save()' error.
但到目前为止还可以.
然后我在 bookController 的保存操作中遇到了同样的问题.但这一次,变通办法没有奏效.
I then encountered the same problem in the save action of my bookController. But this time, the workaround didn't do the trick.
有什么建议吗?
我知道,我可以重命名 id,但随后我将不得不更改所有脚手架视图...
I known, I can rename the id, but then I will have to change all scaffolded views...
推荐答案
这是数据绑定的一个特性.您不希望提交的数据能够更改诸如 id
和 version
之类的托管字段,因此您使用的 Map 构造函数绑定了除这两个之外的所有可用属性(它还会忽略 class
、metaClass
和其他一些的任何值.
That's a feature of databinding. You don't want submitted data to be able to change managed fields like id
and version
, so the Map constructor that you're using binds all available properties except those two (it also ignores any value for class
, metaClass
, and a few others).
所以这里有点不匹配,因为该值不是由 Hibernate/GORM 管理的,而是由您管理的.如您所见,解决方法是您需要分两步而不是一步来创建对象.
So there's a bit of a mismatch here since the value isn't managed by Hibernate/GORM but by you. As you saw the workaround is that you need to create the object in two steps instead of just one.
这篇关于我自己在 GORM 中的 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!