Hibernate custom UUID (mysqluuid_short) _ MySQL

Source: Internet
Author: User
Tags guid identifier
Hibernate custom UUID (mysqluuid_short) Hibernate

BitsCN.com

UUID generation class

3 import org. apache. commons. logging. log; 4 import org. apache. commons. logging. logFactory; 5 import org. hibernate. hibernateException; 6 import org. hibernate. mappingException; 7 import org. hibernate. dialect. dialect; 8 import org. hibernate. engine. sessionImplementor; 9 import org. hibernate. exception. JDBCExceptionHelper; 10 import org. hibernate. id. retriable; 11 import org. hibernate. id. identifierGenerator; 12 import org. hibernate. type. type; 13 14 import java. io. serializable; 15 import java. SQL. preparedStatement; 16 import java. SQL. resultSet; 17 import java. SQL. SQLException; 18 import java. util. properties; 19 20/** 21 * Created with IntelliJ IDEA.22 * User: Administrator23 * Date: 13-5-824 * Time: * To change this template use File | Settings | File Templates.26 */27 public class ShortUUIDIncrementGenerator implements IdentifierGenerator, retriable {28 private static final Log log = LogFactory. getLog (ShortUUIDIncrementGenerator. class); 29 private final String SQL = "select uuid_short ()"; 30 31 @ Override32 public Serializable generate (SessionImplementor sessionImplementor, Object o) throws HibernateException {33 synchronized (this) {34 try {35 PreparedStatement st = sessionImplementor. getBatcher (). prepareSelectStatement (SQL); 36 try {37 ResultSet rs = st.exe cuteQuery (); 38 final long result; 39 try {40 rs. next (); 41 result = rs. getLong (1); 42} finally {43 rs. close (); 44} 45 log. debug ("GUID identifier generated:" + result); 46 return result; 47} finally {48 sessionImplementor. getBatcher (). closeStatement (st); 49} 50} catch (SQLException e) {51 throw JDBCExceptionHelper. convert (52 sessionImplementor. getFactory (). getSQLExceptionConverter (), 53 e, 54 "cocould not retrieve GUID", 55 sql56); 57} 58} 59} 60 61 @ Override62 public void configure (Type type, Properties properties, dialect dialect) throws MappingException {63 // To change body of implemented methods use File | Settings | File Templates.64} 65 66}

Configuration:

    @Id    @GeneratedValue(generator = "shortUid")    @GenericGenerator(name = "shortUid", strategy = "com.up72.msi.util.ShortUUIDIncrementGenerator")    @Column(name = "id", unique = true, nullable = false, insertable = true, updatable = true, length = 19)    public java.lang.Long getId() {

BitsCN.com

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.