1 package org.hibernate.id; 3 4 import java.io.Serializable ; 5 import java.sql.ResultSet ; 6 import java.sql.SQLException ; 7 import java.util.HashMap ; 8 import java.util.Properties ; 9 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 import org.hibernate.HibernateException; 13 import org.hibernate.MappingException; 14 import org.hibernate.dialect.Dialect; 15 import org.hibernate.type.Type; 16 import org.hibernate.util.ReflectHelper; 17 18 23 public final class IdentifierGeneratorFactory { 24 25 private static final Log log = LogFactory.getLog(IdentifierGeneratorFactory.class); 26 27 30 public static Serializable getGeneratedIdentity(ResultSet rs, Type type) 31 throws SQLException , HibernateException, IdentifierGenerationException { 32 if ( !rs.next() ) { 33 throw new HibernateException( "The database returned no natively generated identity value" ); 34 } 35 final Serializable id = IdentifierGeneratorFactory.get( rs, type ); 36 37 if ( log.isDebugEnabled() ) log.debug( "Natively generated identity: " + id ); 38 return id; 39 } 40 41 public static Serializable get(ResultSet rs, Type type) 43 throws SQLException , IdentifierGenerationException { 44 45 Class clazz = type.getReturnedClass(); 46 if ( clazz==Long .class ) { 47 return new Long ( rs.getLong(1) ); 48 } 49 else if ( clazz==Integer .class ) { 50 return new Integer ( rs.getInt(1) ); 51 } 52 else if ( clazz==Short .class ) { 53 return new Short ( rs.getShort(1) ); 54 } 55 else if ( clazz==String .class ) { 56 return rs.getString(1); 57 } 58 else { 59 throw new IdentifierGenerationException("this id generator generates long, integer, short or string"); 60 } 61 62 } 63 64 private static final HashMap GENERATORS = new HashMap (); 65 66 public static final Serializable SHORT_CIRCUIT_INDICATOR = new Serializable () { 67 public String toString() { return "SHORT_CIRCUIT_INDICATOR"; } 68 }; 69 70 public static final Serializable POST_INSERT_INDICATOR = new Serializable () { 71 public String toString() { return "POST_INSERT_INDICATOR"; } 72 }; 73 74 static { 75 GENERATORS.put("uuid", UUIDHexGenerator.class); 76 GENERATORS.put("hilo", TableHiLoGenerator.class); 77 GENERATORS.put("assigned", Assigned.class); 78 GENERATORS.put("identity", IdentityGenerator.class); 79 GENERATORS.put("select", SelectGenerator.class); 80 GENERATORS.put("sequence", SequenceGenerator.class); 81 GENERATORS.put("seqhilo", SequenceHiLoGenerator.class); 82 GENERATORS.put("increment", IncrementGenerator.class); 83 GENERATORS.put("foreign", ForeignGenerator.class); 84 GENERATORS.put("guid", GUIDGenerator.class); 85 GENERATORS.put("uuid.hex", UUIDHexGenerator.class); } 87 88 public static IdentifierGenerator create(String strategy, Type type, Properties params, Dialect dialect) 89 throws MappingException { 90 try { 91 Class clazz = getIdentifierGeneratorClass( strategy, dialect ); 92 IdentifierGenerator idgen = (IdentifierGenerator) clazz.newInstance(); 93 if (idgen instanceof Configurable) ( (Configurable) idgen).configure(type, params, dialect); 94 return idgen; 95 } 96 catch (Exception e) { 97 throw new MappingException("could not instantiate id generator", e); 98 } 99 } 100 101 public static Class getIdentifierGeneratorClass(String strategy, Dialect dialect) { 102 Class clazz = (Class ) GENERATORS.get(strategy); 103 if ( "native".equals(strategy) ) clazz = dialect.getNativeIdentifierGeneratorClass(); 104 try { 105 if (clazz==null) clazz = ReflectHelper.classForName(strategy); 106 } 107 catch (ClassNotFoundException e) { 108 throw new MappingException("could not interpret id generator strategy: " + strategy); 109 } 110 return clazz; 111 } 112 113 public static Number createNumber(long value, Class clazz) throws IdentifierGenerationException { 114 if ( clazz==Long .class ) { 115 return new Long (value); 116 } 117 else if ( clazz==Integer .class ) { 118 return new Integer ( (int) value ); 119 } 120 else if ( clazz==Short .class ) { 121 return new Short ( (short) value ); 122 } 123 else { 124 throw new IdentifierGenerationException("this id generator generates long, integer, short"); 125 } 126 } 127 128 private IdentifierGeneratorFactory() {} 130 } 131 | Popular Tags |