---
title: "GRAILS ID GENERATOR 可以自己设置ID字段的值"
author: "seanwon"
date: "2009-03-02T17:21:20Z"
tags: []
categories: ["企业架构"]
slug: "grails-id-generator-id"
draft: false
lastmod: "2009-03-25T13:24:23Z"
id: 341710
url: "https://seanwon.iteye.com/blog/341710"
---

**这个bug已经在1.1正式版本修复，可以用insert:true来指定是新建对象。**

必须在mapping里面指定：

id generator:'assigned', params:

不然会提示，没有找到此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版本是生效的。