Primary Key Generation class
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. retriable; 13 Import Org. hibernate. Id. identifiergenerator; 14 Import Org. hibernate. type. type; 15 /** 16 * 17 * Class Name: generatepk 18 * Class description: automatically generates a custom primary key based on hibernate. 19 * Created by: andy_lj 20 * Creation Time: 11:40:50 AM 21 * Modify remarks: 22 * @ Version 23 */ 24 Public Class Generatepk Implements Retriable, identifiergenerator { 25 Public String sign; // User in user000000001 26 Public String classname; // Object Class Name 27 Public String PK; // Primary Key name 28 Public String idlength;// Length of user000000001 29 30 /** 31 * Get the custom value in 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 * Generate a primary key 43 */ 44 @ Override 45 Public Serializable generate (sessionimplementor arg0, object arg1) 46 Throws Hibernateexception { 47 // Obtain the length of the primary key 48 Int Leng = Integer. valueof (idlength ); 49 // The maximum ID number in the database to be queried 50 Stringbuffer SQL = New Stringbuffer ("select max (." ). Append (PK) 51 . Append (") from" ) 52 . Append (classname) 53 . Append ("as a where ." ) 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 // If it is the first time to add a record, it is similar to 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 } // This is not the first operation, and the record length does not exceed the length read from the configuration file 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 } // This is not the first operation. The record length exceeds the length read from the configuration file. 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 }
Configuration File: 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 >