hibernate 3 ID策略生成器自定义,可用于注释 - 规则: 九位业务编号 + 六位日期 + 六位自增长序列

简介: /***hibernate ID策略生成器 自定义 -  规则: 业务编号 + 日期 + 六位自增长序列*/public class MyKeyGenerator implements IdentifierGenerator, Configurable {    private static final Log log = LogFactory.
/**
*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;
 }

目录
相关文章
|
SQL Java 数据库连接
《Hibernate上课笔记》-----class8----Hibernate的检索方式和检索策略
《Hibernate上课笔记》-----class8----Hibernate的检索方式和检索策略
83 0
《Hibernate上课笔记》-----class8----Hibernate的检索方式和检索策略
|
Oracle 关系型数据库 Java
hibernate注解 映射序列到Oracle
hibernate注解 映射序列到Oracle
|
XML Java 数据库连接
2021-5-13讲课内容hibernate主键id映射_XML方式
概述 项目结构 Student类 hibernate.cfg.xml log4j.properties Student.hbm.xml StudentTest类 理论 常用的五种方式 1. increment: 2. identity主键自增 3.sequence 序列 4. native 5. uuid
201 0
2021-5-13讲课内容hibernate主键id映射_XML方式
|
存储 SQL Oracle
Hibernate-05-主键生成策略
Hibernate-05-主键生成策略
Hibernate-05-主键生成策略
|
SQL 缓存 Java
hibernate(八) Hibernate检索策略(类级别,关联级别,批量检索)详解
很多看起来很难的东西其实并不难,关键是看自己是否花费了时间和精力去看,如果一个东西你能看得懂,同样的,别人也能看得懂,体现不出和别人的差距,所以当你觉得自己看了很多书或者学了很多东西的时候,你要想想,你花费的也就那么一点时间,别人花你这么多时间也能够学到你所学到的东西,所以还是要继续努力。既然不是天才,唯有靠勤奋来弥补。
164 0
|
SQL 关系型数据库 数据库
Hibernate-ORM:03.Hibernate主键生成策略
  ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------       此篇博客简单记录五种常用的主键生成策咯:   不同的主键生成策略,生成的sql语句,以及hibernate的操作都是不同的!   3.
1235 0
|
Oracle 关系型数据库 Java
Hibernate主键生成策略及选择
1 .increment:适用于short,int,long作为主键,不是使用数据库自动增长机制 这是hibernate中提供的一种增长机制            在程序运行时,先进行查询:select max(id) from user;                              ...
741 0
|
Web App开发 Java 数据库连接