KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: AbstractPostInsertGenerator.java,v 1.7 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.hibernate.HibernateException;
10 import org.hibernate.engine.SessionImplementor;
11 import org.hibernate.exception.JDBCExceptionHelper;
12 import org.hibernate.pretty.MessageHelper;
13
14 /**
15  * @author Gavin King
16  */

17 public abstract class AbstractPostInsertGenerator implements PostInsertIdentifierGenerator{
18
19     public Serializable JavaDoc generate(SessionImplementor s, Object JavaDoc obj) {
20         return IdentifierGeneratorFactory.POST_INSERT_INDICATOR;
21     }
22     
23     protected abstract String JavaDoc getSQL(PostInsertIdentityPersister persister);
24     
25     protected void bindParameters(SessionImplementor session, PreparedStatement JavaDoc ps, Object JavaDoc object, PostInsertIdentityPersister persister)
26     throws SQLException JavaDoc {}
27     
28     protected abstract Serializable JavaDoc getResult(SessionImplementor session, ResultSet JavaDoc rs, Object JavaDoc object, PostInsertIdentityPersister persister)
29     throws SQLException JavaDoc;
30
31     public Serializable JavaDoc getGenerated(SessionImplementor session, Object JavaDoc object, PostInsertIdentityPersister persister)
32     throws HibernateException {
33         
34         final String JavaDoc sql = getSQL(persister);
35
36         try {
37     
38             //fetch the generated id in a separate query
39
PreparedStatement JavaDoc idSelect = session.getBatcher().prepareStatement(sql);
40             try {
41                 bindParameters(session, idSelect, object, persister);
42                 ResultSet JavaDoc rs = idSelect.executeQuery();
43                 try {
44                     return getResult(session, rs, object, persister);
45                 }
46                 finally {
47                     rs.close();
48                 }
49             }
50             finally {
51                 session.getBatcher().closeStatement(idSelect);
52             }
53     
54         }
55         catch ( SQLException JavaDoc sqle ) {
56             throw JDBCExceptionHelper.convert(
57                     session.getFactory().getSQLExceptionConverter(),
58                     sqle,
59                     "could not insert: " + MessageHelper.infoString( persister ),
60                     sql
61                 );
62         }
63
64     }
65
66 }
67
Popular Tags