1. Use a 128-bit UUID algorithm to generate a string-type identifier.
2. Unique in a network (an IP address is used to generate an algorithm ).
3. UUID is encoded as a 32-bit hexadecimal number string.The corresponding primary key field of the database should be varchar or char type, 32 bits, if less than 32 bits, you may encounter "ORA-01401: the inserted value is too large.
4. It is not recommended to write UUID. HEX directly as UUID.UUID is no longer supported since hibernate3.0. string, view changelog, you can find: Changes in version 3.0 Beta 1 (21.12.2004) * removed UUID. string and renamed UUID. HEX to plain UUID. In the hibernate3.x API, The abstractuuidgenerator class is only a uuidhexgenerator subclass. In the hibernate ing file, set it to <generator class = "UUID"/>; (actually written as UUID. hex is also usable, but the official reference document contains UUID, so it is not recommended to write it as UUID. HEX)
V. UUID. HEX Generation Algorithm in hibernate
Package COM. wallimn. util; </P> <p> Import Java. io. serializable; <br/> Import java.net. inetaddress; <br/>/** <br/> * unique primary key generation method. Extracted from hibernate. <Br/> */<br/> public class uuidgenerator {</P> <p> Private Static final int IP address; <br/> Public static int iptoint (byte [] bytes) {<br/> int result = 0; <br/> for (INT I = 0; I <4; I ++) {<br/> result = (result <8)-byte. min_value + (INT) bytes [I]; <br/>}< br/> return result; <br/>}< br/> static {<br/> int ipadd; <br/> try {<br/> ipadd = iptoint (inetaddress. getlocalhost (). getaddress (); <br/>}< br/> catch (exception e) {<br/> ipadd = 0; <br/>}< br/> IP = ipadd; <br/>}< br/> Private Static short counter = (short) 0; <br/> Private Static final int JVM = (INT) (system. currenttimemillis () >>> 8); </P> <p> Public uuidgenerator () {<br/>}</P> <p>/** <br/> * unique compression SS JVMs on this machine (unless they load this class <br/> * in same quater second-very unlikely) <br/> */<br/> protected int getjvm () {<br/> return JVM; <br/>}</P> <p>/** <br/> * unique in a millisecond for this JVM instance (unless there <br/> * are> short. max_value instances created in a millisecond) <br/> */<br/> protected short getcount () {<br/> synchronized (uuidgenerator. class) {<br/> If (counter <0) Counter = 0; <br/> return counter ++; <br/>}</P> <p>/** <br/> * unique in a local network <br/> */<br/> protected int getip () {<br/> return IP; <br/>}</P> <p>/** <br/> * unique down to millisecond <br/> */<br/> protected short gethitime () {<br/> return (short) (system. currenttimemillis () >>> 32); <br/>}< br/> protected int getlotime () {<br/> return (INT) system. currenttimemillis (); <br/>}</P> <p> private final static string Sep = ""; </P> <p> protected string format (INT intval) {<br/> string formatted = integer. tohexstring (intval); <br/> stringbuffer Buf = new stringbuffer ("00000000"); <br/> Buf. replace (8-formatted.length (), 8, formatted); <br/> return Buf. tostring (); <br/>}</P> <p> protected string format (short shortval) {<br/> string formatted = integer. tohexstring (shortval); <br/> stringbuffer Buf = new stringbuffer ("0000"); <br/> Buf. replace (4-formatted.length (), 4, formatted); <br/> return Buf. tostring (); <br/>}</P> <p> Public serializable generate () {<br/> return New stringbuffer (36) <br/>. append (format (getip ())). append (SEP) <br/>. append (format (getjvm ())). append (SEP) <br/>. append (format (gethitime ())). append (SEP) <br/>. append (format (getlotime ())). append (SEP) <br/>. append (format (getcount () <br/>. tostring (); <br/>}</P> <p>}
The algorithm is referenced. Address: http://wallimn.javaeye.com/blog/327844