我在GORM的自己的ID [英] my own id in GORM

查看:250
本文介绍了我在GORM的自己的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在grails中更改标准的'id':

 调用Book {
String id
String title

static mapping {
id generator:'assigned'
}
}

不幸的是,我很快注意到,这打破了我的引导。而不是

 新书(id:'some ISBN',title:'great book')。save(flush:true, 



我不得不使用



b.id ='some ISBN'
b.save(flush:true,failOnError: true)

否则,我得到一个'ids for this class must be manually assigned before save()'错误。

但到目前为止没有问题。



然后,我在保存操作中遇到了同样的问题BookController的。但这一次,解决方法没有办法。



有什么建议吗?



我知道,我可以重命名id,但是之后我将不得不更改所有脚手架的视图...

解决方案

这是数据绑定的一个特性。您不希望提交的数据能够更改像 id version 这样的托管字段,因此Map构造函数你正在使用绑定所有可用的属性,除了那两个(它也忽略了任何值为 class metaClass 和a很少有人)。

所以这里有一些不匹配,因为这个值不是由Hibernate / GORM管理的,而是由你来管理的。正如您所看到的,解决方法是您需要分两步创建对象,而不是一个。


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)

I had to use

def b = new Book(title:'great book')
b.id = 'some ISBN'
b.save(flush:true, failOnError:true)

otherwise I get an 'ids for this class must be manually assigned before calling save()' error.

but that's ok so far.

I then encountered the same problem in the save action of my bookController. But this time, the workaround didn't do the trick.

Any suggestions?

I known, I can rename the id, but then I will have to change all scaffolded views...

解决方案

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).

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆