如何在java中生成字母数字ID? [英] How to generate an alphanumeric ID in java?

查看:664
本文介绍了如何在java中生成字母数字ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为GROUPS的表,它有一列 GROUP_ID ,其值为


GRP001,GRP002, GRP003


等等。现在每当我插入一个新行时,我必须插入一个
(当前)GROUP_ID =(最高)GROUP_ID + 1,例如,如果最高GROUP_ID = GRP003当我插入新的时候,我必须生成一个新的GROUP_ID GRP004行。



我怎样才能使用java?



我现在使用Hibernate和Struts 2一起我的程序

有什么方法可以使用hibernate来处理这个问题吗?或者我将不得不编写额外的代码来锁定表,检查数据库的最大ID(然后增加它),并最终释放锁?

解决方案

我记得一次解决类似这样的问题。我所做的是我创建了一个由hibernate支持的自定义主键生成器。



这个人在这里清楚地解释它:自定义Hibernate主键生成器



基本上你只需要实现 org.hibernate.id.IdentifierGenerator 并且应该设置所有的元素。



请注意,上述示例中实施的解决方案与数据库相关。但我认为有时候常识应该胜过过度工程。

I have a table named GROUPS which has a column GROUP_ID whose values are

GRP001, GRP002, GRP003

and so on. Now every time I insert a new row I have to insert it with a (current) GROUP_ID= (highest)GROUP_ID+ 1 , for example if highest GROUP_ID= GRP003 I have to generate a new GROUP_ID GRP004 when I insert a new row.

How can I do this using java?

I am currently using Hibernate along with Struts 2 in my program

Is there any way to deal with this using hibernate? Or will I have to write additional code to lock the table, check the db for max Id (and then increment it) and finally release the lock?

解决方案

I remember solving a problem similar to this once. What I did was I create a custom primary key generator as supported by hibernate.

This guy explains it clearly here: "Custom Hibernate Primary Key Generator"

Basically you just need to implement org.hibernate.id.IdentifierGenerator and all should be set.

Just be aware that the solution implemented in the example above is database dependent. But I think sometimes common sense should prevail over overengineering.

这篇关于如何在java中生成字母数字ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆