uuid.hex主键生成器


对于数据库主键生成策略,大家都了解一些,尤其是关于hibernate的主键生成更是方便很多。

而大多数人只知道使用,hibernate给定的生成策略,我今天想说的是主键生成器,就是自己写一个类来生成主键。

在开始之前,先对hibernate给定的生成策略做个了解。



**************************************************************************************************

1  assigned:主键由外部程序负责生成,无需hibernate参与。特点是:主键的生成完全由用户决定,与底层数据库无关,用户需要维护主键值,并且要在session.save()之前指定主键值,否则会抛出异常。

2  hilo:通过hilo算法实现的主键生成机制,需要额外的数据库表保存主键生成历史状态。使用高低位算法生成主键,高低位算法使用一个高位值和一个低位值,然后把算法得到的两个值拼接起来作为数据库中的唯一主键。默认情况下采用的表是hibernate_unique_key。

3  seqhilo:与hilo类似,通过hilo算法实现的主键生成机制,只是主键历史状态保存在Sequence中,适用于支持Sequence的数据库

4  increment:主键按数值顺序递增。当HIbernate准备在数据库中插入一条记录时,首先从数据库表中取出当前主键字段的最大值,然后在最大值的基础上加1,作为当前持久化对象的标识符属性值。这种方式可能产生的问题是:如果当前有多个实例访问数据库,那么由于各个实例各自维持着主键状态,不同实例可能生成相同的主键,从而造成主键重复异常。

5  identity:采用数据库提供的主键生成机制

6  sequence:采用数据库提供的sequence生成主键,要设定序列名  <param name="sequence">name_seq</param>。如果未指定序列名,则hibernate默认使用名为“hibernate_sequence"的序列

7  native:有Hibernate根据地层数据库自行判断采用identity,hilo,sequence中一种作为主键生成机制

8  uuid.hex:由hibernate基于128位唯一值产生算法生成十六进制数值作为主键

9  uuid.string:与uuid.hex相似,只是生成的主键没有进行编码(16位),在某些数据库中可能出现问题

 10  foreign:使用外部表的字段作为主键

11  select:使用触发器生成主键

**************************************************************************************************

现在我想说的是UUID主键生成。

在hibernate的映射文件中,配置成<generator class="uuid"/>就可以直接使用了。

关于它,要知道几点知识:


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

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

三、UUID被编码为一个32位16进制数字的字符串。


好了,现在我们想自己写一个UUID主键生成器该怎么办呢?

其实很简单,既然Hibernate已经帮我们写好了,我们就可以看看它的源码怎么写的,单独把相关代码摘出来就可以了。


在hibernate3.jar中,UUIDHexGenerator

/*jadclipse*/// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.

package org.hibernate.id;

import java.io.PrintStream;
import java.io.Serializable;
import java.util.Properties;
import org.hibernate.Hibernate;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.type.Type;
import org.hibernate.util.PropertiesHelper;

// Referenced classes of package org.hibernate.id:
//            AbstractUUIDGenerator, Configurable, IdentifierGenerator

public class UUIDHexGenerator extends AbstractUUIDGenerator
    implements Configurable
{

    public UUIDHexGenerator()
    {
        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(SessionImplementor session, Object obj)
    {
        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();
    }

    public void configure(Type type, Properties params, Dialect d)
    {
        sep = PropertiesHelper.getString("separator", params, "");
    }

    public static void main(String args[])
        throws Exception
    {
        Properties props = new Properties();
        props.setProperty("separator", "/");
        IdentifierGenerator gen = new UUIDHexGenerator();
        ((Configurable)gen).configure(Hibernate.STRING, props, null);
        IdentifierGenerator gen2 = new UUIDHexGenerator();
        ((Configurable)gen2).configure(Hibernate.STRING, props, null);
        for(int i = 0; i < 10; i++)
        {
            String id = (String)gen.generate(null, null);
            System.out.println(id);
            String id2 = (String)gen2.generate(null, null);
            System.out.println(id2);
        }

    }

    private String sep;
}


/*
	DECOMPILATION REPORT

	Decompiled from: D:\ChinaDevelopmentBankJBPM\workSpace\frame\webapp\WEB-INF\lib\hibernate3.jar
	Total time: 195 ms
	Jad reported messages/errors:
	Exit status: 0
	Caught exceptions:
*/

这个类继承了AbstractUUIDGenerator

/*jadclipse*/// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.

package org.hibernate.id;

import java.net.InetAddress;
import org.hibernate.util.BytesHelper;

// Referenced classes of package org.hibernate.id:
//            IdentifierGenerator

public abstract class AbstractUUIDGenerator
    implements IdentifierGenerator
{

    public AbstractUUIDGenerator()
    {
    }

    protected int getJVM()
    {
        return JVM;
    }

    protected short getCount()
    {
        Class class1 = org.hibernate.id.AbstractUUIDGenerator.class;
        JVM INSTR monitorenter ;
        if(counter < 0)
            counter = 0;
        return counter++;
        Exception exception;
        exception;
        throw exception;
    }

    protected int getIP()
    {
        return IP;
    }

    protected short getHiTime()
    {
        return (short)(int)(System.currentTimeMillis() >>> 32);
    }

    protected int getLoTime()
    {
        return (int)System.currentTimeMillis();
    }

    private static final int IP;
    private static short counter = 0;
    private static final int JVM = (int)(System.currentTimeMillis() >>> 8);

    static 
    {
        int ipadd;
        try
        {
            ipadd = BytesHelper.toInt(InetAddress.getLocalHost().getAddress());
        }
        catch(Exception e)
        {
            ipadd = 0;
        }
        IP = ipadd;
    }
}


/*
	DECOMPILATION REPORT

	Decompiled from: D:\ChinaDevelopmentBankJBPM\workSpace\frame\webapp\WEB-INF\lib\hibernate3.jar
	Total time: 156 ms
	Jad reported messages/errors:
Overlapped try statements detected. Not all exception handlers will be resolved in the method getCount
Couldn't fully decompile method getCount
Couldn't resolve all exception handlers in method getCount
	Exit status: 0
	Caught exceptions:
*/

要想摘出来代码,要明白:继承的父类是必须要摘的,而接口我们完全可以忽略。

再把父类中的方法摘出放到子类中去,这样这个父类也没有利用价值了。

到此基本上快完成了,对于几个出错的方法,我们尽可能注释掉,最后不出错,写个main方法测试一下。



这是最后摘出来整合好的UUIDHexGenerator

package org.base.pk;

import java.io.Serializable;
import java.net.InetAddress;


public class UUIDHexGenerator {

	public UUIDHexGenerator() {
		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();
	}

	

	private String sep;

	// /

	protected int getJVM() {
		return JVM;
	}

	protected short getCount() {
		Class class1 = org.base.pk.UUIDHexGenerator.class;
		if (counter < 0)
			counter = 0;
		return counter++;
	}

	protected int getIP() {
		return IP;
	}

	protected short getHiTime() {
		return (short) (int) (System.currentTimeMillis() >>> 32);
	}

	protected int getLoTime() {
		return (int) System.currentTimeMillis();
	}

	private static final int IP;
	private static short counter = 0;
	private static final int JVM = (int) (System.currentTimeMillis() >>> 8);

	static {
		int ipadd;
		try {
			ipadd = toInt(InetAddress.getLocalHost().getAddress());
		} catch (Exception e) {
			ipadd = 0;
		}
		IP = ipadd;
	}
	public static int toInt(byte bytes[])
    {
        int result = 0;
        for(int i = 0; i < 4; i++)
            result = ((result << 8) - -128) + bytes[i];

        return result;
    }
	
	
	public static void main(String args[]) throws Exception {
		UUIDHexGenerator gen = new UUIDHexGenerator();
		for (int i = 0; i < 10; i++) {
			String id = (String) gen.generate();
			System.out.println(id);
		}

	}
}

运行一下,打印:

8ab78fab3cf123ac013cf123acb00000
8ab78fab3cf123ac013cf123acb10001
8ab78fab3cf123ac013cf123acb10002
8ab78fab3cf123ac013cf123acb10003
8ab78fab3cf123ac013cf123acb10004
8ab78fab3cf123ac013cf123acb10005
8ab78fab3cf123ac013cf123acb10006
8ab78fab3cf123ac013cf123acb10007
8ab78fab3cf123ac013cf123acb10008
8ab78fab3cf123ac013cf123acb10009


到此为止,还不算大功告成,毕竟这是一个主键生成器,算是工具类了。

我们要把这个类改成静态的,使用起来才算方便。

改变时注意变量sep要赋值空字符串,否则这个变量始终是null。

这是最终版的UUID主键生成器

package org.base.pk;

import java.io.Serializable;
import java.net.InetAddress;


public class UUIDHexGenerator {

	public UUIDHexGenerator() {}

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

	public static 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 static 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();
	}

	

	public static String sep="";

	// /

	public static int getJVM() {
		return JVM;
	}

	public static short getCount() {
		Class class1 = org.base.pk.UUIDHexGenerator.class;
		if (counter < 0)
			counter = 0;
		return counter++;
	}

	public static int getIP() {
		return IP;
	}

	public static short getHiTime() {
		return (short) (int) (System.currentTimeMillis() >>> 32);
	}

	public static int getLoTime() {
		return (int) System.currentTimeMillis();
	}

	private static final int IP;
	private static short counter = 0;
	private static final int JVM = (int) (System.currentTimeMillis() >>> 8);

	static {
		int ipadd;
		try {
			ipadd = toInt(InetAddress.getLocalHost().getAddress());
		} catch (Exception e) {
			ipadd = 0;
		}
		IP = ipadd;
	}
	public static int toInt(byte bytes[])
    {
        int result = 0;
        for(int i = 0; i < 4; i++)
            result = ((result << 8) - -128) + bytes[i];

        return result;
    }
	
	
	public static void main(String args[]) throws Exception {
		for (int i = 0; i < 10; i++) {
			String id = (String) UUIDHexGenerator.generate();
			System.out.println(id);
		}

	}
}

运行后打印:

8ab78fab3cf12cc3013cf12cc4020000
8ab78fab3cf12cc3013cf12cc4020001
8ab78fab3cf12cc3013cf12cc4020002
8ab78fab3cf12cc3013cf12cc4020003
8ab78fab3cf12cc3013cf12cc4020004
8ab78fab3cf12cc3013cf12cc4020005
8ab78fab3cf12cc3013cf12cc4020006
8ab78fab3cf12cc3013cf12cc4020007
8ab78fab3cf12cc3013cf12cc4020008
8ab78fab3cf12cc3013cf12cc4030009

大功告成。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值