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 import java.util.Properties ; 9 10 import org.hibernate.MappingException; 11 import org.hibernate.dialect.Dialect; 12 import org.hibernate.engine.SessionImplementor; 13 import org.hibernate.type.Type; 14 15 24 public class SelectGenerator extends AbstractPostInsertGenerator implements Configurable { 25 26 private String uniqueKeyPropertyName; 27 private Type idType; 28 private String entityName; 29 30 public void configure(Type type, Properties params, Dialect d) throws MappingException { 31 uniqueKeyPropertyName = params.getProperty("key"); 32 entityName = params.getProperty(ENTITY_NAME); 33 this.idType = type; 34 } 35 36 protected String getSQL(PostInsertIdentityPersister persister) { 37 return persister.getSelectByUniqueKeyString(uniqueKeyPropertyName); 38 } 39 40 protected void bindParameters(SessionImplementor session, PreparedStatement ps, Object object, PostInsertIdentityPersister persister) 41 throws SQLException { 42 Type uniqueKeyPropertyType = session.getFactory() 43 .getClassMetadata(entityName) 44 .getPropertyType(uniqueKeyPropertyName); 45 Object uniqueKeyValue = persister.getPropertyValue( object, uniqueKeyPropertyName, session.getEntityMode() ); 46 uniqueKeyPropertyType.nullSafeSet( ps, uniqueKeyValue, 1, session ); 47 } 48 49 protected Serializable getResult(SessionImplementor session, ResultSet rs, Object object, PostInsertIdentityPersister persister) 50 throws SQLException { 51 if ( !rs.next() ) { 52 throw new IdentifierGenerationException( "the inserted row could not be located by the unique key: " + uniqueKeyPropertyName ); 53 } 54 return (Serializable ) idType.nullSafeGet(rs, persister.getRootTableKeyColumnNames(), session, object); 55 } 56 } 57 58 59 60 61 62 63 | Popular Tags |