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 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 17 public abstract class AbstractPostInsertGenerator implements PostInsertIdentifierGenerator{ 18 19 public Serializable generate(SessionImplementor s, Object obj) { 20 return IdentifierGeneratorFactory.POST_INSERT_INDICATOR; 21 } 22 23 protected abstract String getSQL(PostInsertIdentityPersister persister); 24 25 protected void bindParameters(SessionImplementor session, PreparedStatement ps, Object object, PostInsertIdentityPersister persister) 26 throws SQLException {} 27 28 protected abstract Serializable getResult(SessionImplementor session, ResultSet rs, Object object, PostInsertIdentityPersister persister) 29 throws SQLException ; 30 31 public Serializable getGenerated(SessionImplementor session, Object object, PostInsertIdentityPersister persister) 32 throws HibernateException { 33 34 final String sql = getSQL(persister); 35 36 try { 37 38 PreparedStatement idSelect = session.getBatcher().prepareStatement(sql); 40 try { 41 bindParameters(session, idSelect, object, persister); 42 ResultSet 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 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 |