Hibernate ID Generator

Source: Internet
Author: User

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.