01: package org.hibernate.id.insert;
02:
03: import org.hibernate.id.PostInsertIdentityPersister;
04: import org.hibernate.engine.SessionImplementor;
05: import org.hibernate.exception.JDBCExceptionHelper;
06: import org.hibernate.pretty.MessageHelper;
07:
08: import java.io.Serializable;
09: import java.sql.PreparedStatement;
10: import java.sql.SQLException;
11:
12: /**
13: * Abstract InsertGeneratedIdentifierDelegate implementation where the
14: * underlying strategy causes the enerated identitifer to be returned as an
15: * effect of performing the insert statement. Thus, there is no need for an
16: * additional sql statement to determine the generated identitifer.
17: *
18: * @author Steve Ebersole
19: */
20: public abstract class AbstractReturningDelegate implements
21: InsertGeneratedIdentifierDelegate {
22: private final PostInsertIdentityPersister persister;
23:
24: public AbstractReturningDelegate(
25: PostInsertIdentityPersister persister) {
26: this .persister = persister;
27: }
28:
29: public final Serializable performInsert(String insertSQL,
30: SessionImplementor session, Binder binder) {
31: try {
32: // prepare and execute the insert
33: PreparedStatement insert = prepare(insertSQL, session);
34: try {
35: binder.bindValues(insert);
36: return executeAndExtract(insert);
37: } finally {
38: releaseStatement(insert, session);
39: }
40: } catch (SQLException sqle) {
41: throw JDBCExceptionHelper.convert(session.getFactory()
42: .getSQLExceptionConverter(), sqle,
43: "could not insert: "
44: + MessageHelper.infoString(persister),
45: insertSQL);
46: }
47: }
48:
49: protected PostInsertIdentityPersister getPersister() {
50: return persister;
51: }
52:
53: protected abstract PreparedStatement prepare(String insertSQL,
54: SessionImplementor session) throws SQLException;
55:
56: protected abstract Serializable executeAndExtract(
57: PreparedStatement insert) throws SQLException;
58:
59: protected void releaseStatement(PreparedStatement insert,
60: SessionImplementor session) throws SQLException {
61: session.getBatcher().closeStatement(insert);
62: }
63: }
|