Hibernate 中的自定义序列应每年重置
我正在开发使用 Spring MVC 3 和 hibernate 3 的简单应用程序
。我需要生成以下格式的 ID。
Month/Year/number(number will increase).
月份和年份值确实会根据当前日期而变化。 一旦一年结束,假设一个财政年度结束后,序列再次从头开始,如月/年/数字。希望我说清楚了。
示例:
1st Financial Year:
Mar/2011/001
Jun/2011/002
Dec/2011/003
Feb/2011/004
...
...
2nd Financial Year:
Mar/2012/001
Mar/2012/002
Aug/2012/003
..
..
..
我该如何解决这个问题? 请帮我。
I'm working on simple application that uses Spring MVC 3 and hibernate 3.
I have a requirement to generate ID in the following format.
Month/Year/number(number will increase).
Month and year values do change based on the current Date.
Once the year completed, let say after the completion of one financial year the sequence again start from the beginning, like month/Year/number. Hope I'm clear.
Example:
1st Financial Year:
Mar/2011/001
Jun/2011/002
Dec/2011/003
Feb/2011/004
...
...
2nd Financial Year:
Mar/2012/001
Mar/2012/002
Aug/2012/003
..
..
..
How can I approach this problem?
Please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建自定义序列生成器。您需要在某处保存一条记录,用于存储最后保存的记录的年份。每次您想要生成新的标识符时,请检查该值,如果保存的最后一条记录不是今年的,请更新该值并重新创建下面使用的序列。如果该值尚未设置,您可以通过查询最新记录的日期来计算它。
一些伪代码:P
Create a custom sequence generator. You will need to keep somewhere a record that stores the year of the last record saved. Each time you want to gerenate a new identifier check that value and if the last record saved is not from this year update that value and recreate the sequence being used underneath. If that value is not yet set you can calculate it querying the date of the newest record.
A bit of pseudocode :P
查看此处提供的各种选项在休眠文档中。
这是相关的 stackoverflow 线程。
最后,您的实现必须源自此 hibernate接口,然后你可以在generate方法中实现你的自定义逻辑。
Look at various options provided here in the hibernate docs.
Here is a related stackoverflow thread.
Finally, your implementation will have to derive from this hibernate interface and then you can implement your custom logic in the generate method.