⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hibidgenerator.java

📁 jsp + struts + spring + hibernate实例
💻 JAVA
字号:
package com.easyjf.example.business.hibernate;

import java.io.Serializable;
import java.util.Properties;
import java.util.Random;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.Query;
import org.hibernate.dialect.Dialect;
import org.hibernate.type.Type;

public class HibIdGenerator implements org.hibernate.id.Configurable,org.hibernate.id.IdentifierGenerator {
	private String column,table;
	private Integer length;
	private Object obj;
public synchronized Serializable generate(SessionImplementor session, Object obj) 
	throws HibernateException {
	this.obj=obj;
	return nextValue(session);	
	}	
public  void configure(Type type, Properties params, Dialect dialect)
			throws MappingException {		
		table = params.getProperty("table");		
		column = params.getProperty("column");
		length=new Integer(params.getProperty("length"));
		}
private synchronized String nextValue(SessionImplementor session)
{
	StringBuffer id=new StringBuffer(""+(new java.util.Date().getTime()));
	Random r=new Random(10);
	id.append(Math.abs(r.nextInt()));	
	if(!check(id.substring(0,length.intValue()),session))return nextValue(session);
	else return id.substring(0,length.intValue());	
}
private synchronized boolean check(String id,SessionImplementor session)
{
		boolean r=true;		
		String hql="select count(*) from "+obj.getClass().getName()+" where "+column+"='"+id+"'";
		Query query1=session.createQuery(hql);	
		int rows=Integer.parseInt(query1.uniqueResult().toString());		
		if(rows>0)r=false;
		return r;
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -