Source code: org/hibernate/id/insert/AbstractReturningDelegate.java
1 package org.hibernate.id.insert;
2
3 import org.hibernate.id.PostInsertIdentityPersister;
4 import org.hibernate.engine.SessionImplementor;
5 import org.hibernate.exception.JDBCExceptionHelper;
6 import org.hibernate.pretty.MessageHelper;
7
8 import java.io.Serializable;
9 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 InsertGeneratedIdentifierDelegate {
21 private final PostInsertIdentityPersister persister;
22
23 public AbstractReturningDelegate(PostInsertIdentityPersister persister) {
24 this.persister = persister;
25 }
26
27 public final Serializable performInsert(String insertSQL, SessionImplementor session, Binder binder) {
28 try {
29 // prepare and execute the insert
30 PreparedStatement insert = prepare( insertSQL, session );
31 try {
32 binder.bindValues( insert );
33 return executeAndExtract( insert );
34 }
35 finally {
36 releaseStatement( insert, session );
37 }
38 }
39 catch ( SQLException sqle ) {
40 throw JDBCExceptionHelper.convert(
41 session.getFactory().getSQLExceptionConverter(),
42 sqle,
43 "could not insert: " + MessageHelper.infoString( persister ),
44 insertSQL
45 );
46 }
47 }
48
49 protected PostInsertIdentityPersister getPersister() {
50 return persister;
51 }
52
53 protected abstract PreparedStatement prepare(String insertSQL, SessionImplementor session) throws SQLException;
54
55 protected abstract Serializable executeAndExtract(PreparedStatement insert) throws SQLException;
56
57 protected void releaseStatement(PreparedStatement insert, SessionImplementor session) throws SQLException {
58 session.getBatcher().closeStatement( insert );
59 }
60 }