首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

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

2012-11-05 
GRAILS ID GENERATOR 可以自己设置ID字段的值这个bug已经在1.1正式版本修复,可以用insert:true来指定是新

GRAILS ID GENERATOR 可以自己设置ID字段的值
这个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版本是生效的。

热点排行