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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM