在HIBERNATE中,identity ,sequence ,native 是数据局提供的主键生成方式,往往也不是我们需要,而且在程序跨数据库方面也体现出不足.还有基于算法的生成方式生成出来的主键基本都是字符串的.
我们现在需要一种生成方式:使用Long作为主键类型,需要自动增,支持集群.那么我们需要自定义一个我们的主键生成器才能实现了.
主键生成器代码 类:
package com.ta.jiang;
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);
}
}
}
}
代码二:
Hibernate 配置文件:
<id name="id" type="long" column="id" >
<generator class="com.ta.jiang.IncrementGenerator" />
</id>
分享到:
相关推荐
关于Hibernate的各种主键生成策略与配置详解
1主键常用的生成策略 2对数据库的依赖性总结 3关于主键生成策略的选择 详细解释
hibernate中自动生成主键的办法
struts+hibernate+自定义标签 做的分页模板
hibernate的联合主键怎么设置、hibernate的联合主键怎么设置.doc
hibernate 无主键表映射资源说明: 1. 简单Demo 2. Demo对应的sql语句 3. 对应博客文档
hibernate自定义查询
hibernate中基于主键的one2one,关键看看配置文件是怎么配置的,很有帮助,案例简单,深入浅出
Hibernate中主键生成策略,开发的时候经常会用到的我都整理到这里了~
hibernate复合主键设置,里面很全面的配置,可以拿到直接用
本文是讲解的是hibernate3.2的主键生成方式,通过annotation来实现,详细的分析了主键在hibernate的生成以及在真实项目的运用。。。。
NULL 博文链接:https://paladin1988.iteye.com/blog/1639102
Hibernate联合主键,MyEclipse的例子
NULL 博文链接:https://zhangzhi199129.iteye.com/blog/1663919
hibernate中用到联合主键的看看就会了很棒的资料
hibernate的主键生成策略介绍,详细介绍了有关hibernate的常用主键生成策略,希望对一些正在学习hibernate的同学有一定的帮助
demo代码; 博客地址:http://blog.csdn.net/smilefyx/article/details/48951933
Hibernate各种主键生成策略与配置详解 1、assigned 主键由外部程序负责生成,在 save() 之前必须指定一个。Hibernate不负责维护主键生成。与Hibernate和底层数据库都无关,可以跨数据库。在存储对象前,必须要使用...
Hibernate主键生成方式 1.identity方式 2.increment方式 3.assigned方式 4.sequence方式 5.native方式 6.UUID方式