KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: SelectGenerator.java,v 1.5 2005/05/27 03:53:59 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 import java.util.Properties JavaDoc;
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 /**
16  * An IdentityGenerator that selects the inserted row, to determine
17  * an identifier value assigned by the database. The correct row is
18  * located using a unique key.
19  * <br><br>
20  * One mapping parameter is required: key.
21  *
22  * @author Gavin King
23  */

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