Grails:生成自定义 ID
我有一个域对象 Issue
,它有一个属性 Date createTime
,我想通过一些自定义逻辑将其用作 GORM 的 id。
例如,假设第一个 Issue
于 2011/08/02
创建,则 ID
应为 2011080201
。同一天的下一个Issue
应该有ID
2011080202
等等。
那么,第二天2011/08/03
创建的第一期ID
应该是2011080301
。
因此,基本要求是我必须将递增的值附加到 createTime
属性上。
应该采取什么方法??
I have a domain object Issue
that has a property Date createTime
, that I want to be used as the 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??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信你必须创建一个自定义 id 生成器。
尝试创建一个实现 org.hibernate.id.IdentifierGenerator 接口的类 my.package.MyCustomIdGenerator。
然后尝试像这样设置您的域类。
上面的语法可能不正确。但值得一试。
看一下这里:http: //grails.org/doc/1.0.x/guide/5.%20Object%20Relational%20Mapping%20(GORM).html
和此处:
http://docs.jboss.org /hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-id
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.
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