KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > id > GUIDGenerator


1 //$Id: GUIDGenerator.java,v 1.11 2005/06/22 04:19:31 oneovthafew Exp $
2
package org.hibernate.id;
3
4 import java.io.Serializable JavaDoc;
5 import java.sql.PreparedStatement JavaDoc;
6 import java.sql.ResultSet JavaDoc;
7 import java.sql.SQLException JavaDoc;
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 /**
16  * Generates <tt>string</tt> values using the SQL Server NEWID() function.
17  *
18  * @author Joseph Fifield
19  */

20 public class GUIDGenerator implements IdentifierGenerator {
21
22     private static final Log log = LogFactory.getLog(GUIDGenerator.class);
23
24     public Serializable JavaDoc generate(SessionImplementor session, Object JavaDoc obj)
25     throws HibernateException {
26         
27         final String JavaDoc sql = session.getFactory().getDialect().getSelectGUIDString();
28         try {
29             PreparedStatement JavaDoc st = session.getBatcher().prepareSelectStatement(sql);
30             try {
31                 ResultSet JavaDoc rs = st.executeQuery();
32                 final String JavaDoc 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 JavaDoc 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