如何生成 字符+数字的自增主键 hibernate+oracle
1个回答
展开全部
解渗磨决方法:继承IdentifierGenerator,Configurable 这2个接口
package com.testplatform.idGenerator;
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.log4j.Logger;
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 IDGenerator implements IdentifierGenerator,Configurable {
private String prefix;
private String seq;
static Logger log4j = Logger.getLogger(IDGenerator.class.getClass());
public Serializable generate(SessionImplementor session, Object arg1)
throws HibernateException {
// TODO Auto-generated method stub
Connection connection = session.connection();
try {
//SELECT seq_date_info_id.nextval from dual;
PreparedStatement ps = connection
.prepareStatement("SELECT "+seq+".nextval from dual");
ResultSet rs = ps.executeQuery();
if (rs.next()) {
int id = rs.getInt("nextval"派亩);
String code = prefix+id;
//丛羡斗String code = prefix + StringUtils.leftPad("" + id,3, '0');
log4j.warn("IDGenerator: " + code);
return code;
}
} catch (SQLException e) {
log4j.warn(e);
throw new HibernateException(
"Unable to generate id ");
}
return null;
}
public void configure(Type arg0, Properties params, Dialect arg2)
throws MappingException {
// TODO Auto-generated method stub
// System.out.println("configure");
/* table = params.getProperty("table");
if (table == null)
table = params.getProperty(PersistentIdentifierGenerator.TABLE);*/
seq = params.getProperty("seq");
prefix = params.getProperty("prefix");
}
}
package com.testplatform.idGenerator;
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.log4j.Logger;
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 IDGenerator implements IdentifierGenerator,Configurable {
private String prefix;
private String seq;
static Logger log4j = Logger.getLogger(IDGenerator.class.getClass());
public Serializable generate(SessionImplementor session, Object arg1)
throws HibernateException {
// TODO Auto-generated method stub
Connection connection = session.connection();
try {
//SELECT seq_date_info_id.nextval from dual;
PreparedStatement ps = connection
.prepareStatement("SELECT "+seq+".nextval from dual");
ResultSet rs = ps.executeQuery();
if (rs.next()) {
int id = rs.getInt("nextval"派亩);
String code = prefix+id;
//丛羡斗String code = prefix + StringUtils.leftPad("" + id,3, '0');
log4j.warn("IDGenerator: " + code);
return code;
}
} catch (SQLException e) {
log4j.warn(e);
throw new HibernateException(
"Unable to generate id ");
}
return null;
}
public void configure(Type arg0, Properties params, Dialect arg2)
throws MappingException {
// TODO Auto-generated method stub
// System.out.println("configure");
/* table = params.getProperty("table");
if (table == null)
table = params.getProperty(PersistentIdentifierGenerator.TABLE);*/
seq = params.getProperty("seq");
prefix = params.getProperty("prefix");
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询