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;
     }

  • 相关阅读:
    aspnet mvc 中 跨域请求的处理方法
    Aspnet Mvc 前后端分离项目手记(三)关于restful 风格Url设计
    Aspnet Mvc 前后端分离项目手记(二)关于token认证
    Aspnet Mvc 前后端分离项目手记(一) 关于跨域问题(还有前言)
    31 | 误删数据后除了跑路,还能怎么办?
    30 | 答疑文章(二):用动态的观点看加锁
    29 | 如何判断一个数据库是不是出问题了?
    28 | 读写分离有哪些坑?
    27 | 主库出问题了,从库怎么办?
    26 | 备库为什么会延迟好几个小时?
  • 原文地址:https://www.cnblogs.com/meetrice/p/1527639.html
Copyright © 2011-2022 走看看