China Open source community
站内导航:
站内排行前50热点文章

精华文章  GDB调试精粹及使用实例
普通文章  STL中map用法详解
精华文章  负载均衡软件比较(Hapr...
普通文章  头文件的重复引用
普通文章  递归函数的调用过程
普通文章  TCP三次握手/四次挥手详解
普通文章  贪心策略的理论基础——...
普通文章  BMH算法原理与实现(模...
普通文章  排列组合与回溯算法
普通文章  DP动态规划
精华文章  Android线程模型
普通文章  Linux socket编程之套接字
普通文章  Linux内核中的红黑树
精华文章  linux下使用minicom的几...
普通文章  Java开源Html解析类库
精华文章  enum类型的本质
普通文章  memcached server LRU ...
普通文章  linux设置环境变量的方法
普通文章  android核心模块及相关...
普通文章  linux源代码包(.tar.g...
普通文章  L.A.M.P配置过程
普通文章  在ubuntu9.10下安装QT4...
普通文章  C/C++程序员常见面试题...
普通文章  gcc编译过程概述
普通文章  python的memcache和jso...
普通文章  应用程序二进制接口---ABI
普通文章  linux内核编译问题
普通文章  Java多线程实现简单实例
普通文章  Python程序员常用的IDE...
普通文章  brk和sbrk详述
普通文章  优化C语言代码(程序员必...
普通文章  python非贪婪,多行匹配...
普通文章  函数指针传递和全局指针...
普通文章  Unix操作系统的历史演变
普通文章  网络编程之C10K问题
普通文章  发行版发布:CentOS 5.4
普通文章  在windows中构建gtk开发...
普通文章  i++循环与i--循环的执行...
普通文章  关于Qvariant类--万能的...
普通文章  Debian sudo 设置
普通文章  busybox1.15.x 交叉编译
普通文章  关于僵死进程zombie
普通文章  递归思想的妙用
普通文章  判断链表是否存在环并找...
普通文章  Android Porting Exper...
普通文章  关于/etc/bashrc和$HOM...
普通文章  [翻译]Django初窥
普通文章  Python list的排序
普通文章  Django实现大数据量分页...
普通文章  Debug方式取代printf满...

 
 
 
当前位置: 首页 >> 程序设计 >> Hibernate中UUID的生成算法
 
 

Hibernate中UUID的生成算法

作者:      来源:http://wallimn.bokee.com     发表时间:2006-12-09     浏览次数:      字号:    

/***********本人原创,欢迎转载,转载请保留本人信息*************/
作者:王力猛 (wallimn)
电邮:wallimn@sohu.com
博客:http://wallimn.bokee.com
   http://blog.csdn.net/wallimn
时间:2006-11-15
/***********本人原创,欢迎转载,转载请保留本人信息*************/

  不知大家在写数据库程序的时候使用什么做主键,使用无意义的东东做主键是个很好的做法,很多人用SEQUENCE,可我觉得Hibernate中的UUID是个不错的选择,我把他剥出来,喜欢的人可以拿去用。
  算法如下:

 package com.xxh.hz.util;

import java.io.Serializable;
import java.net.InetAddress;
/**
 * 唯一主键生成办法。从Hibernate中提取出来。我一直觉得这不错,不用建什么Sequence
 * @version      :  V1.0
 * @author       : 王力猛(Email: wallimn@sohu.com  QQ: 54871876)
 * @date           : 2006-12-8 下午11:51:43
 */
public class UUIDGenerator {

 private static final int IP;
 public static int IptoInt( byte[] bytes ) {
  int result = 0;
  for (int i=0; i<4; i++) {
   result = ( result << 8 ) - Byte.MIN_VALUE + (int) bytes[i];
  }
  return result;
 }
 static {
  int ipadd;
  try {
   ipadd = IptoInt( InetAddress.getLocalHost().getAddress() );
  }
  catch (Exception e) {
   ipadd = 0;
  }
  IP = ipadd;
 }
 private static short counter = (short) 0;
 private static final int JVM = (int) ( System.currentTimeMillis() >>> 8 );

 public UUIDGenerator() {
 }

 /**
  * Unique across JVMs on this machine (unless they load this class
  * in the same quater second - very unlikely)
  */
 protected int getJVM() {
  return JVM;
 }

 /**
  * Unique in a millisecond for this JVM instance (unless there
  * are > Short.MAX_VALUE instances created in a millisecond)
  */
 protected short getCount() {
  synchronized(UUIDGenerator.class) {
   if (counter<0) counter=0;
   return counter++;
  }
 }

 /**
  * Unique in a local network
  */
 protected int getIP() {
  return IP;
 }

 /**
  * Unique down to millisecond
  */
 protected short getHiTime() {
  return (short) ( System.currentTimeMillis() >>> 32 );
 }
 protected int getLoTime() {
  return (int) System.currentTimeMillis();
 }
 
 private final static String sep = "";

 protected String format(int intval) {
  String formatted = Integer.toHexString(intval);
  StringBuffer buf = new StringBuffer("00000000");
  buf.replace( 8-formatted.length(), 8, formatted );
  return buf.toString();
 }

 protected String format(short shortval) {
  String formatted = Integer.toHexString(shortval);
  StringBuffer buf = new StringBuffer("0000");
  buf.replace( 4-formatted.length(), 4, formatted );
  return buf.toString();
 }

 public Serializable generate() {
  return new StringBuffer(36)
   .append( format( getIP() ) ).append(sep)
   .append( format( getJVM() ) ).append(sep)
   .append( format( getHiTime() ) ).append(sep)
   .append( format( getLoTime() ) ).append(sep)
   .append( format( getCount() ) )
   .toString();
 }

}

编辑 webmaster

 
 
 
评论
 
 
发表
 
姓名: QQ:
性别: MSN:
E-mail: 主页:
评分: 1 2 3 4 5
评论内容:
验证码:
  
  • 请遵守《互联网电子公告服务管理规定》及中华人民共和国其他各项有关法律法规。
  • 严禁发表危害国家安全、损害国家利益、破坏民族团结、破坏国家宗教政策、破坏社会稳定、侮辱、诽谤、教唆、淫秽等内容的评论 。
  • 用户需对自己在使用本站服务过程中的行为承担法律责任(直接或间接导致的)。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表网友个人观点,与本网站立场无关。
  •  
    中国源码网 - www.YuanMa.org - 中国 开放源代码+编程 社区