Grails:生成自定义 ID

发布于 2024-11-27 20:27:01 字数 440 浏览 8 评论 0原文

我有一个域对象 Issue ,它有一个属性 Date createTime ,我想通过一些自定义逻辑将其用作 GORM 的 id。

例如,假设第一个 Issue2011/08/02 创建,则 ID 应为 2011080201。同一天的下一个Issue应该有ID2011080202等等。

那么,第二天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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰雪梦之恋 2024-12-04 20:27:01

我相信你必须创建一个自定义 id 生成器。

尝试创建一个实现 org.hibernate.id.IdentifierGenerator 接口的类 my.package.MyCustomIdGenerator。

然后尝试像这样设置您的域类。

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

上面的语法可能不正确。但值得一试。

看一下这里: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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文