hibernate中自定义主键生成器
2014-02-28 23:09
369 查看
Hibernate(目前使用的版本是3.2)中提供了多种生成主键的方式。
然而当前的这么多种生成方式未必能满足我们的要求。
比如increment,可以在一个hibernate实例的应用上很方便的时候,但是在集群的时候就不行了。
再如 identity ,sequence ,native 是数据局提供的主键生成方式,往往也不是我们需要,而且在程序跨数据库方面也体现出不足。
还有基于算法的生成方式生成出来的主键基本都是字符串的。
我们现在需要一种生成方式:使用Long作为主键类型,自动增,支持集群。
那么我们需要自定义一个我们的主键生成器才能实现了。
实现代码:
package hibernate;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.id.PersistentIdentifierGenerator;
import org.hibernate.type.Type;
public class IncrementGenerator implements IdentifierGenerator, Configurable {
private static final Log log = LogFactory.getLog(IncrementGenerator.class);
private Long next;
private String sql;
public Serializable generate(SessionImplementor session, Object object)
throws HibernateException {
if (sql!=null) {
getNext( session.connection() );
}
return next;
}
public void configure(Type type, Properties params, Dialect d) throws MappingException {
String table = params.getProperty("table");
if (table==null) table = params.getProperty(PersistentIdentifierGenerator.TABLE);
String column = params.getProperty("column");
if (column==null) column = params.getProperty(PersistentIdentifierGenerator.PK);
String schema = params.getProperty(PersistentIdentifierGenerator.SCHEMA);
sql = "select max("+column +") from " + ( schema==null ? table : schema + '.' + table );
log.info(sql);
}
private void getNext(Connection conn) throws HibernateException {
try {
PreparedStatement st = conn.prepareStatement(sql);
ResultSet rs = st.executeQuery();
if ( rs.next() ) {
next = rs.getLong(1) + 1;
}
else {
next = 1l;
}
}catch(SQLException e)
{
throw new HibernateException(e);
}
finally {
try{
conn.close();
}catch(SQLException e)
{
throw new HibernateException(e);
}
}
}
}
配置:
在对应的hbm文件里面将id的配置如下:
<id name="id" type="long" column="id" >
<generator class="hibernate.IncrementGenerator" />
</id>
然而当前的这么多种生成方式未必能满足我们的要求。
比如increment,可以在一个hibernate实例的应用上很方便的时候,但是在集群的时候就不行了。
再如 identity ,sequence ,native 是数据局提供的主键生成方式,往往也不是我们需要,而且在程序跨数据库方面也体现出不足。
还有基于算法的生成方式生成出来的主键基本都是字符串的。
我们现在需要一种生成方式:使用Long作为主键类型,自动增,支持集群。
那么我们需要自定义一个我们的主键生成器才能实现了。
实现代码:
package hibernate;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.id.PersistentIdentifierGenerator;
import org.hibernate.type.Type;
public class IncrementGenerator implements IdentifierGenerator, Configurable {
private static final Log log = LogFactory.getLog(IncrementGenerator.class);
private Long next;
private String sql;
public Serializable generate(SessionImplementor session, Object object)
throws HibernateException {
if (sql!=null) {
getNext( session.connection() );
}
return next;
}
public void configure(Type type, Properties params, Dialect d) throws MappingException {
String table = params.getProperty("table");
if (table==null) table = params.getProperty(PersistentIdentifierGenerator.TABLE);
String column = params.getProperty("column");
if (column==null) column = params.getProperty(PersistentIdentifierGenerator.PK);
String schema = params.getProperty(PersistentIdentifierGenerator.SCHEMA);
sql = "select max("+column +") from " + ( schema==null ? table : schema + '.' + table );
log.info(sql);
}
private void getNext(Connection conn) throws HibernateException {
try {
PreparedStatement st = conn.prepareStatement(sql);
ResultSet rs = st.executeQuery();
if ( rs.next() ) {
next = rs.getLong(1) + 1;
}
else {
next = 1l;
}
}catch(SQLException e)
{
throw new HibernateException(e);
}
finally {
try{
conn.close();
}catch(SQLException e)
{
throw new HibernateException(e);
}
}
}
}
配置:
在对应的hbm文件里面将id的配置如下:
<id name="id" type="long" column="id" >
<generator class="hibernate.IncrementGenerator" />
</id>
相关文章推荐
- 最常用的8款 PHP 调试工具,你用过吗?
- spring jdbc mapper
- ZeroMQ(java)之Requerst/Response模式
- zoj2686
- Flex利用socket实现断点续传(二)
- Balanced Binary Tree
- C++编译器与链接器工作原理
- ajax test
- 不足,莫慌,请扬起自信
- 标签的使用
- Singleton
- uva 1554 - Binary Search(暴力)
- 为什么拷贝构造函数必须为引用传递,不能是值传递?
- Maven构建Java工程
- 可靠、高吞吐架构基础改造
- Python实现的简单计算器
- adb opendir failed ,permission denied
- MAC锁屏方法
- uva 10134 - AutoFish(simulation)
- Paste Deployment简介以及cinder-api-paste.ini的解析(1)