您好,欢迎访问一九零五行业门户网

hibernate 自定义UUID(mysql uuid_short)_MySQL

hibernate
bitscn.com
uuid生成类
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.configurable;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: 下午6:1525 * to change this template use file | settings | file templates.26 */27 public class shortuuidincrementgenerator implements identifiergenerator, configurable {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.executequery();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 could 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 }
配置:
@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
其它类似信息

推荐信息