1 /*
2 * Hibernate, Relational Persistence for Idiomatic Java
3 *
4 * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
5 * indicated by the @author tags or express copyright attribution
6 * statements applied by the authors. All third-party contributions are
7 * distributed under license by Red Hat Middleware LLC.
8 *
9 * This copyrighted material is made available to anyone wishing to use, modify,
10 * copy, or redistribute it subject to the terms and conditions of the GNU
11 * Lesser General Public License, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this distribution; if not, write to:
20 * Free Software Foundation, Inc.
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301 USA
23 *
24 */
25 package org.hibernate.id.insert;
26
27 import org.hibernate.id.PostInsertIdentityPersister;
28 import org.hibernate.engine.SessionImplementor;
29 import org.hibernate.exception.JDBCExceptionHelper;
30 import org.hibernate.pretty.MessageHelper;
31
32 import java.io.Serializable;
33 import java.sql.PreparedStatement;
34 import java.sql.SQLException;
35
36 /**
37 * Abstract InsertGeneratedIdentifierDelegate implementation where the
38 * underlying strategy causes the enerated identitifer to be returned as an
39 * effect of performing the insert statement. Thus, there is no need for an
40 * additional sql statement to determine the generated identitifer.
41 *
42 * @author Steve Ebersole
43 */
44 public abstract class AbstractReturningDelegate implements InsertGeneratedIdentifierDelegate {
45 private final PostInsertIdentityPersister persister;
46
47 public AbstractReturningDelegate(PostInsertIdentityPersister persister) {
48 this.persister = persister;
49 }
50
51 public final Serializable performInsert(String insertSQL, SessionImplementor session, Binder binder) {
52 try {
53 // prepare and execute the insert
54 PreparedStatement insert = prepare( insertSQL, session );
55 try {
56 binder.bindValues( insert );
57 return executeAndExtract( insert );
58 }
59 finally {
60 releaseStatement( insert, session );
61 }
62 }
63 catch ( SQLException sqle ) {
64 throw JDBCExceptionHelper.convert(
65 session.getFactory().getSQLExceptionConverter(),
66 sqle,
67 "could not insert: " + MessageHelper.infoString( persister ),
68 insertSQL
69 );
70 }
71 }
72
73 protected PostInsertIdentityPersister getPersister() {
74 return persister;
75 }
76
77 protected abstract PreparedStatement prepare(String insertSQL, SessionImplementor session) throws SQLException;
78
79 protected abstract Serializable executeAndExtract(PreparedStatement insert) throws SQLException;
80
81 protected void releaseStatement(PreparedStatement insert, SessionImplementor session) throws SQLException {
82 session.getBatcher().closeStatement( insert );
83 }
84 }