hibernate 3 ID策略生成器自定义,可用于注释 - 规则: 九位业务编号 + 六位日期 + 六位自增长序列
2009-07-21 11:34
459 查看
/**
*hibernate ID策略生成器 自定义 - 规则: 业务编号 + 日期 + 六位自增长序列
*/
public class MyKeyGenerator implements IdentifierGenerator, Configurable {
private static final Log log = LogFactory.getLog(MyKeyGenerator.class);
private long next;
private String sql;
private String table;
private String column;
private String schema;
public synchronized Serializable generate(SessionImplementor session,
Object object) throws HibernateException {
SimpleDateFormat f = new SimpleDateFormat("yyMMdd");
String preDate = f.format(new Date());
LogAction logaction = new LogAction();
// String bh = logaction.getBh();
String bh = "123456789";
return bh + preDate + getNext(session, bh,table);
}
public void configure(org.hibernate.type.Type type, Properties params,
Dialect d) throws MappingException {
table = params.getProperty("table");
if (table == null)
table = params.getProperty(PersistentIdentifierGenerator.TABLE);
column = params.getProperty("column");
if (column == null)
column = params.getProperty(PersistentIdentifierGenerator.PK);
schema = params.getProperty(PersistentIdentifierGenerator.SCHEMA);
}
/**
* 得到当前表ID的最后六位的最大数
*
* @param session
* @param jsbh
* @return
*/
private String getNext(SessionImplementor session, String bh,String table) {
sql = "select max(substr("+column+",16)) from "+(schema == null ? table : schema + '.' + table)+" where substr("+column+",10,6) = to_char(sysdate,'yyMMdd') and substr("+column+",0,9) = '" + bh + "' and length("+column+")=21 ";
log.info("fetching initial value: " + sql);
try {
PreparedStatement st = session
.getBatcher()
.prepareSelectStatement(
sql);
try {
ResultSet rs = st.executeQuery();
try {
if (rs.next()) {
next = rs.getLong(1) + 1;
if (rs.wasNull())
next = 1;
} else {
next = 1;
}
sql = null;
log.debug("first free id: " + next);
} finally {
rs.close();
}
} finally {
session.getBatcher().closeStatement(st);
}
return toString(6, next);
} catch (SQLException sqle) {
throw JDBCExceptionHelper.convert(session.getFactory()
.getSQLExceptionConverter(), sqle,
"could not fetch initial value for increment generator",
sql);
}
}
/**
* 格式化数字不足补齐
*
* @param num
* @param value
* @return
*/
public static String toString(int num, long value) {
String result = (new Long(value)).toString();
while (num > result.length()) {
result = "0" + result;
}
return result;
}
JAVA中注解使用方法:
@Id @GeneratedValue(generator="custom-id")
@GenericGenerator(name="custom-id", strategy = "javacommon.base.AwdKeyGenerator")
@Column(name = "ID", unique = true, nullable = false, insertable = true, updatable = true, length = 21)
public java.lang.String getId() {
return this.id;
}
*hibernate ID策略生成器 自定义 - 规则: 业务编号 + 日期 + 六位自增长序列
*/
public class MyKeyGenerator implements IdentifierGenerator, Configurable {
private static final Log log = LogFactory.getLog(MyKeyGenerator.class);
private long next;
private String sql;
private String table;
private String column;
private String schema;
public synchronized Serializable generate(SessionImplementor session,
Object object) throws HibernateException {
SimpleDateFormat f = new SimpleDateFormat("yyMMdd");
String preDate = f.format(new Date());
LogAction logaction = new LogAction();
// String bh = logaction.getBh();
String bh = "123456789";
return bh + preDate + getNext(session, bh,table);
}
public void configure(org.hibernate.type.Type type, Properties params,
Dialect d) throws MappingException {
table = params.getProperty("table");
if (table == null)
table = params.getProperty(PersistentIdentifierGenerator.TABLE);
column = params.getProperty("column");
if (column == null)
column = params.getProperty(PersistentIdentifierGenerator.PK);
schema = params.getProperty(PersistentIdentifierGenerator.SCHEMA);
}
/**
* 得到当前表ID的最后六位的最大数
*
* @param session
* @param jsbh
* @return
*/
private String getNext(SessionImplementor session, String bh,String table) {
sql = "select max(substr("+column+",16)) from "+(schema == null ? table : schema + '.' + table)+" where substr("+column+",10,6) = to_char(sysdate,'yyMMdd') and substr("+column+",0,9) = '" + bh + "' and length("+column+")=21 ";
log.info("fetching initial value: " + sql);
try {
PreparedStatement st = session
.getBatcher()
.prepareSelectStatement(
sql);
try {
ResultSet rs = st.executeQuery();
try {
if (rs.next()) {
next = rs.getLong(1) + 1;
if (rs.wasNull())
next = 1;
} else {
next = 1;
}
sql = null;
log.debug("first free id: " + next);
} finally {
rs.close();
}
} finally {
session.getBatcher().closeStatement(st);
}
return toString(6, next);
} catch (SQLException sqle) {
throw JDBCExceptionHelper.convert(session.getFactory()
.getSQLExceptionConverter(), sqle,
"could not fetch initial value for increment generator",
sql);
}
}
/**
* 格式化数字不足补齐
*
* @param num
* @param value
* @return
*/
public static String toString(int num, long value) {
String result = (new Long(value)).toString();
while (num > result.length()) {
result = "0" + result;
}
return result;
}
JAVA中注解使用方法:
@Id @GeneratedValue(generator="custom-id")
@GenericGenerator(name="custom-id", strategy = "javacommon.base.AwdKeyGenerator")
@Column(name = "ID", unique = true, nullable = false, insertable = true, updatable = true, length = 21)
public java.lang.String getId() {
return this.id;
}
相关文章推荐
- Hibernate 和 JPA 注解方式自定义ID生成器
- 160727、自定义hibernate主键生成策略生成字符串+数字自增长
- [原创]java WEB学习笔记81:Hibernate学习之路--- 对象关系映射文件(.hbm.xml):hibernate-mapping 节点,class节点,id节点(主键生成策略),property节点,在hibernate 中 java类型 与sql类型之间的对应关系,Java 时间和日期类型的映射,Java 大对象类型 的 映射 (了解),映射组成关系
- 注解实现hibernate 主键ID自增长生成策略
- Hibernate之自定义ID生成器
- Hibernate初学者---自定义生成ID策略的步骤
- Oracle主键ID设置自动增长(序列+触发器)
- MongoDB自动增长id实现、自定义函数调用、与Spring集成
- hibernate annotation注解 主键ID自增长
- Hibernate 第五讲 数据库Id生成策略 (5)
- JBPM “无效数字” 异常 与 Hibernate ID策略
- hibernate的主键增长策略
- hibernate利用mysql的从增张id属性实现自增长id和手动赋值id并存
- Hibernate配置及三种Id生成策略
- hibernate 中id生成策略
- 【Hibernate框架开发之四】Hibernate-Annotation常用的注解归总&&ID的生成策略&&联合主键
- ID的生成策略(hibernate的id生成策略,主键类为什么需要实现序列化接口,同时还要重写hashCode()和equals()方法)
- 策略模式和自定义排序规则
- Rhythmk 学习 Hibernate 03 - Hibernate 之 延时加载 以及 ID 生成策略
- Hibernate的ID主键生成策略