Hibernate生成UUID算法

一、用一个128-bit的UUID算法生成字符串类型的标识符。

二、在一个网络中唯一(生成算法使用了IP地址)。

三、UUID被编码为一个32位16进制数字的字符串。
数据库相应的主键字段应该是varchar或者char型,32位,如果少于32位,在增加记录的时候你也许会遇到“ORA-01401: 插入的值对于列过大”的错误。
四、不推荐写成uuid.hex 推荐直接写成uuid 
从hibernate3.0开始已经不再支持 uuid.string,查看changelog可以发现: Changes in version 3.0 beta 1 (21.12.2004) * removed uuid.string and renamed uuid.hex to plain uuid,hibernate3.x的api中AbstractUUIDGenerator类只有UUIDHexGenerator子类了,使用时在 hibernate的映射文件中,配置成<generator class="uuid"/>;(其实写成uuid.hex也是可以用的,但官方的reference文档中是uuid,所以不推荐写成 uuid.hex)

五、hibernate中uuid.hex的生成算法

[java:showcolumns] view plain copy
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1.   package  com.wallimn.util;    
  2.     
  3. import  java.io.Serializable;    
  4. import  java.net.InetAddress;    
  5. /**   
  6.  * 唯一主键生成办法。从Hibernate中提取出来。   
  7.  */     
  8. public   class  UUIDGenerator {    
  9.     
  10.  private   static   final   int  IP;    
  11.  public   static   int  IptoInt(  byte [] bytes ) {    
  12.   int  result =  0 ;    
  13.   for  ( int  i= 0 ; i< 4 ; i++) {    
  14.    result = ( result << 8  ) - Byte.MIN_VALUE + ( int ) bytes[i];    
  15.   }    
  16.   return  result;    
  17.  }    
  18.  static  {    
  19.   int  ipadd;    
  20.   try  {    
  21.    ipadd = IptoInt( InetAddress.getLocalHost().getAddress() );    
  22.   }    
  23.   catch  (Exception e) {    
  24.    ipadd = 0 ;    
  25.   }    
  26.   IP = ipadd;    
  27.  }    
  28.  private   static   short  counter = ( short0 ;    
  29.  private   static   final   int  JVM = ( int ) ( System.currentTimeMillis() >>>  8  );    
  30.     
  31.  public  UUIDGenerator() {    
  32.  }    
  33.     
  34.  /**   
  35.   * Unique across JVMs on this machine (unless they load this class   
  36.   * in the same quater second - very unlikely)   
  37.   */     
  38.  protected   int  getJVM() {    
  39.   return  JVM;    
  40.  }    
  41.     
  42.  /**   
  43.   * Unique in a millisecond for this JVM instance (unless there   
  44.   * are > Short.MAX_VALUE instances created in a millisecond)   
  45.   */     
  46.  protected   short  getCount() {    
  47.   synchronized (UUIDGenerator. class ) {    
  48.    if  (counter< 0 ) counter= 0 ;    
  49.    return  counter++;    
  50.   }    
  51.  }    
  52.     
  53.  /**   
  54.   * Unique in a local network   
  55.   */     
  56.  protected   int  getIP() {    
  57.   return  IP;    
  58.  }    
  59.     
  60.  /**   
  61.   * Unique down to millisecond   
  62.   */     
  63.  protected   short  getHiTime() {    
  64.   return  ( short ) ( System.currentTimeMillis() >>>  32  );    
  65.  }    
  66.  protected   int  getLoTime() {    
  67.   return  ( int ) System.currentTimeMillis();    
  68.  }    
  69.      
  70.  private   final   static  String sep =  "" ;    
  71.     
  72.  protected  String format( int  intval) {    
  73.   String formatted = Integer.toHexString(intval);    
  74.   StringBuffer buf = new  StringBuffer( "00000000" );    
  75.   buf.replace( 8 -formatted.length(),  8 , formatted );    
  76.   return  buf.toString();    
  77.  }    
  78.     
  79.  protected  String format( short  shortval) {    
  80.   String formatted = Integer.toHexString(shortval);    
  81.   StringBuffer buf = new  StringBuffer( "0000" );    
  82.   buf.replace( 4 -formatted.length(),  4 , formatted );    
  83.   return  buf.toString();    
  84.  }    
  85.     
  86.  public  Serializable generate() {    
  87.   return   new  StringBuffer( 36 )    
  88.    .append( format( getIP() ) ).append(sep)    
  89.    .append( format( getJVM() ) ).append(sep)    
  90.    .append( format( getHiTime() ) ).append(sep)    
  91.    .append( format( getLoTime() ) ).append(sep)    
  92.    .append( format( getCount() ) )    
  93.    .toString();    
  94.  }    
  95.     
  96. }    
 

算法为引用。原文地址:http://wallimn.javaeye.com/blog/327844

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值