zoukankan      html  css  js  c++  java
  • hibernate 3 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;
     }

  • 相关阅读:
    多表联合查询,利用 concat 模糊搜索
    order by 中利用 case when 排序
    Quartz.NET 3.0.7 + MySql 动态调度作业+动态切换版本+多作业引用同一程序集不同版本+持久化+集群(一)
    ASP.NET Core 2.2 基础知识(十八) 托管和部署 概述
    ASP.NET Core 2.2 基础知识(十七) SignalR 一个极其简陋的聊天室
    ASP.NET Core 2.2 基础知识(十六) SignalR 概述
    ASP.NET Core 2.2 基础知识(十五) Swagger
    ASP.NET Core 2.2 基础知识(十四) WebAPI Action返回类型(未完待续)
    linux磁盘管理 磁盘查看操作
    linux磁盘管理 文件挂载
  • 原文地址:https://www.cnblogs.com/meetrice/p/1527639.html
Copyright © 2011-2022 走看看