开发者

Grails:Generating Custom ID

I have a domain object Issue that has a property Date createTime, that I want to be used as the开发者_JS百科 id by GORM with some custom logic.

For example suppose the first Issue is created on 2011/08/02, the ID should be 2011080201. The next Issue on the same day should have the ID 2011080202 and so on.

Then, the first issue created on the following day 2011/08/03 the ID should be 2011080301.

So the basic requirement is I have to append an incremented value on to the createTime property.

What should be the approach??


I believe you have to create a custom id generator.

Try creating a class my.package.MyCustomIdGenerator which implements the interface org.hibernate.id.IdentifierGenerator.

then try to setup your domain class like this.

static mapping = {    
    id generator:'my.package.MyCustomIdGenerator', params:[...]    
}

The above syntax may be incorrect. but it's worth a shot.

Take a look here: http://grails.org/doc/1.0.x/guide/5.%20Object%20Relational%20Mapping%20(GORM).html

and here:

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-id

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜