Generator class
Package jet. Framework. hibernate;
Import com. Youyang. UI. helper. constants;
Import jet. Framework. util. dateutils;
Import org. Apache. log4j. Logger;
Import org. hibernate. hibernateexception;
Import org. hibernate. mappingexception;
Import org. hibernate. dialect. dialect;
Import org. hibernate. Engine. sessionimplementor;
Import org. hibernate. Id. retriable;
Import org. hibernate. Id. identifiergenerator;
Import org. hibernate. Id. persistentidentifiergenerator;
Import org. hibernate. type. type;
Import java. Io. serializable;
Import java. SQL. connection;
Import java. SQL. preparedstatement;
Import java. SQL. resultset;
Import java. SQL. sqlexception;
Import java. util. date;
Import java. util. properties;
Import java. util. RegEx. pattern;
Public class refundidgenerator implements identifiergenerator, retriable {
Private Static final logger = logger. getlogger (refundidgenerator. Class );
Private string next;
Private string SQL;
Public serializable generate (sessionimplementor session, object)
Throws hibernateexception {
If (SQL! = NULL ){
Return getnext (session. Connection ());
} Else {
Return next;
}
}
Public void configure (type, properties Params, dialect d) throws mappingexception {
String table = Params. getproperty ("table ");
If (Table = NULL) Table = Params. getproperty (persistentidentifiergenerator. Table );
String column = Params. getproperty ("column ");
If (column = NULL) column = Params. getproperty (persistentidentifiergenerator. pk );
String schema = Params. getproperty (persistentidentifiergenerator. schema );
SQL = "select max (" + column + ") from" + (schema = NULL? Table: schema + '.' + table );
}
Private string getnext (connection conn) throws hibernateexception {
Try {
Preparedstatement ST = conn. preparestatement (SQL );
Resultset rs = st.exe cutequery ();
Long maxid = 0l;
Stringbuffer tempid = new stringbuffer (constants. refund_key );
If (Rs. Next ()){
Next = Rs. getstring (1 );
Pattern pattern = pattern. Compile ("^ // D * $ ");
If (pattern. matcher (next. substring (next. Length ()-4). Matches ()){
Maxid = long. valueof (next. substring (next. Length ()-4 ));
}
}
Maxid ++;
Tempid. append (dateutils. Format (new date (), "yymm "));
Tempid. append (resetid (maxid. tostring (), constants. refund_key_num, "0 "));
Return tempid. tostring ();
} Catch (sqlexception e ){
Throw new hibernateexception (E );
} Finally {
Try {
Conn. Close ();
} Catch (sqlexception e ){
Throw new hibernateexception (E );
}
}
}
Public static string resetid (string source, int newsize, string replacestring ){
String result = "";
If (source. Length () <newsize ){
For (INT I = 0; I <newsize-source. Length (); I ++ ){
Result + = replacestring;
}
Result = Result + source;
} Else {
Result = source;
}
Return result;
}
}
Id Configuration
@ ID
@ Generatedvalue (generator = "refundidgenerator ")
@ Genericgenerator (name = "refundidgenerator ",
Strategy = "jet. Framework. hibernate. refundidgenerator ")
@ Column (name = "product_order_refund_id ")
Private string productorderrefundid;