1 package org.hibernate.id; 3 4 import java.io.Serializable ; 5 import java.sql.PreparedStatement ; 6 import java.sql.ResultSet ; 7 import java.sql.SQLException ; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import org.hibernate.HibernateException; 12 import org.hibernate.engine.SessionImplementor; 13 import org.hibernate.exception.JDBCExceptionHelper; 14 15 20 public class GUIDGenerator implements IdentifierGenerator { 21 22 private static final Log log = LogFactory.getLog(GUIDGenerator.class); 23 24 public Serializable generate(SessionImplementor session, Object obj) 25 throws HibernateException { 26 27 final String sql = session.getFactory().getDialect().getSelectGUIDString(); 28 try { 29 PreparedStatement st = session.getBatcher().prepareSelectStatement(sql); 30 try { 31 ResultSet rs = st.executeQuery(); 32 final String result; 33 try { 34 rs.next(); 35 result = rs.getString(1); 36 } 37 finally { 38 rs.close(); 39 } 40 log.debug("GUID identifier generated: " + result); 41 return result; 42 } 43 finally { 44 session.getBatcher().closeStatement(st); 45 } 46 } 47 catch (SQLException sqle) { 48 throw JDBCExceptionHelper.convert( 49 session.getFactory().getSQLExceptionConverter(), 50 sqle, 51 "could not retrieve GUID", 52 sql 53 ); 54 } 55 } 56 57 } 58 | Popular Tags |