Hibernate之自定义ID生成器Hibernate之自定义ID生成器
Ò? xxx.hml.xml
;
;
;
;
?þ ?ßÌåʵÏÖ: IdentifierGeneratorImpl.java
Óõ?ÁËcommons???ß?üÖеÄRandomStringUtils
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import org.ap...
Hibernate之自定义ID生成器
Ò? xxx.hml.xml
<;id name=";id"; type=";java.lang.String";>;
<;column name=";id"; length=";22"; />;
<;generator class=";xx.xx.IdentifierGeneratorImpl"; />;
<;/id>;
?þ ?ßÌåʵÏÖ: IdentifierGeneratorImpl.java
Óõ?ÁËcommons???ß?üÖеÄRandomStringUtils
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import org.apache.commons.lang.RandomStringUtils; import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.SessionImplementor; import org.hibernate.id.Configurable;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.type.Type;
public class IdentifierGeneratorImpl implements IdentifierGenerator, Configurable
{
private static final int IDLENG = 32;
private static final String YYMMDDHHMMSS = ";yyyyMMddhhmmss";;
private static String getCurrentDate(){
return new SimpleDateFormat(YYMMDDHHMMSS).format(new Date());
}
public Serializable generate(SessionImplementor session, Object obj) throws
HibernateException {
return new
StringBuilder().append(getCurrentDate()).append(";-";).append(RandomStringUtils.randomNumeric(IDLENG));
}
public void configure(Type type, Properties params, Dialect d) throws MappingException {
}
public static void main(String[] args) {
IdentifierGeneratorImpl pu = new IdentifierGeneratorImpl();
System.out.println(pu.generate(null, null));
/*int random = 32;
System.out.println(RandomStringUtils.randomNumeric(random));
System.out.println(RandomStringUtils.randomAscii(random));
System.out.println(RandomStringUtils.randomAlphabetic(random));
System.out.println(RandomStringUtils.randomAlphanumeric(random));*/
}
}
本文档为【Hibernate之自定义ID生成器】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。