论坛首页 Java企业应用论坛

GRAILS ID GENERATOR 可以自己设置ID字段的值

浏览 2082 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-03-02   最后修改:2009-03-25
这个bug已经在1.1正式版本修复,可以用insert:true来指定是新建对象。

必须在mapping里面指定:

id generator:'assigned', params:[type:'Integer']

不然会提示,没有找到此ID的对象。

有个问题,如果version指定为false,即使指定assigned,
在创建的时候,依然会导致update而不是save。

我解决的方式是用hql的insert来实现的,而没有用.save方法。

如果其他人有更好的方案,请提示,谢谢!

来看看官方给的解释:
Many legacy database files have composite keys and they rarely have a version number or timestamp. The GORM Mapping DSL is fantastic but the Composite ID has an issue when creating a new row. The domain.save() operation causes an SQL update. The problem arises when the version facility is turned off (via version:false) and the Hibernate generator is set to 'assigned' (id generator:'assigned') -- Hibernate is not able to sense that an insert is required.
This plugin adds a method called insert to all domain classes. It is up to the developer to predicate the call to save() or insert().

Note that Grails 1.0.2 provides a more elegant solution. The domain.save() method now accepts domain.save(insert:true) to explicitly predicate the insert operation. So use this plug-in only for Grails applications that are below version 1.0.2.

但是问题是我在1.1beta3版本,尝试使用save(insert:true)依然不起作用。
1.0.4版本是生效的。
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics