hibernate生成自定义主键(类似user000000001——可自己通过配置文件配置)

生成主键的类

View Code
 1 package action;
 2 
 3 import java.io.Serializable;
 4 import java.util.List;
 5 import java.util.Properties;
 6 
 7 import org.hibernate.HibernateException;
 8 import org.hibernate.MappingException;
 9 import org.hibernate.dialect.Dialect;
10 import org.hibernate.engine.QueryParameters;
11 import org.hibernate.engine.SessionImplementor;
12 import org.hibernate.id.Configurable;
13 import org.hibernate.id.IdentifierGenerator;
14 import org.hibernate.type.Type;
15 /**
16  * 
17 * 类名称:      GeneratePK   
18 * 类描述:     在hibernate的基础上自动生成自定义的主键
19 * 创建人:     andy_lj  
20 * 创建时间:2012-08-10 上午11:40:50     
21 * 修改备注:   
22 * @version
23  */
24 public class GeneratePK implements Configurable, IdentifierGenerator {
25     public String sign;// user000000001中的user
26     public String classname; //实体类的类名
27     public String pk;//主键名字
28     public String idLength;//user000000001的长度
29     
30     /**
31      * 取得User.hbm.xml中的自定义的值
32      */
33     @Override
34     public void configure(Type arg0, Properties arg1, Dialect arg2)
35             throws MappingException {
36         this.classname = arg1.getProperty("classname");
37         this.pk = arg1.getProperty("pk");
38         this.sign = arg1.getProperty("sign");
39         this.idLength = arg1.getProperty("idLength");
40     }
41     /**
42      * 生成主键
43      */
44     @Override
45     public Serializable generate(SessionImplementor arg0, Object arg1)
46             throws HibernateException {
47          //获得主键的长度
48         int leng = Integer.valueOf(idLength);
49         //需要查询数据库中最大的ID号
50         StringBuffer sql = new StringBuffer("select max(a.").append(pk)
51                                                             .append(") from ")
52                                                             .append(classname)
53                                                             .append(" as a where a.")
54                                                             .append(pk)
55                                                             .append(" like '")
56                                                             .append(sign)
57                                                             .append("%'");
58         QueryParameters qp = new QueryParameters();
59         List ls = arg0.list(sql.toString(), qp);
60         String max = (String) ls.get(0);
61         int i = 0;
62         //如果是第一次添加记录那么就是类似user000000001
63         if (max == null || max.trim().equals("")) {
64             max = "1";
65             for(; i < leng-sign.length()-1; i++) {
66                 max = "0" + max;
67             }
68             i = 0;
69             return sign + max;
70         }//不是第一次的操作,并且记录的长度没有超过从配置文件中读取的长度
71         else if(max != null && max.length() <= leng) {
72             max = max.replaceAll(sign, "");
73             Integer imax = Integer.parseInt(max) + 1;
74             String returnnum = String.valueOf(imax);
75             int zero = leng-sign.length()-returnnum.length();
76             for(; i < zero; i++) {
77                 returnnum = "0" + returnnum;
78             }
79             i = 0;
80             return sign + returnnum;
81         }//不是第一次的操作,记录的长度超过了从配置文件中读取的长度
82         else {
83             leng = max.length();
84             max = max.replaceAll(sign, "");
85             Integer imax = Integer.parseInt(max) + 1;
86             String returnnum = String.valueOf(imax);
87             int zero = leng-sign.length()-returnnum.length();
88             for(; i < zero; i++) {
89                 returnnum = "0" + returnnum;
90             }
91             return sign + returnnum;
92         }
93     }
94 
95 }

 

配置文件:User.hbm.xml

View Code
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC 
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping package="entity">
 6     <class name="User" table="TEMP" lazy="false">
 7         <id name="userId" column="userId">
 8             <generator class="action.GeneratePK" >
 9               <param name="classname">User</param>
10               <param name="pk">userId</param>
11               <param name="sign">poli</param>
12               <param name="idLength">13</param>
13             </generator>
14         </id>
15         <property name="name" column="name" />
16         <property name="age" column="age" type="integer"/>
17         <property name="address" column="address" />
18     </class>
19 </hibernate-mapping>

 

posted on 2012-08-10 12:20  lovebeauty  阅读(3111)  评论(1编辑  收藏  举报

导航