001/*
002 * Copyright 2002-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.orm.hibernate3;
018
019import java.sql.SQLException;
020
021import org.hibernate.HibernateException;
022import org.hibernate.Session;
023
024/**
025 * Callback interface for Hibernate code. To be used with {@link HibernateTemplate}'s
026 * execution methods, often as anonymous classes within a method implementation.
027 * A typical implementation will call {@code Session.load/find/update} to perform
028 * some operations on persistent objects. It can also perform direct JDBC operations
029 * via Hibernate's {@code Session.connection()}, operating on a JDBC Connection.
030 *
031 * <p>Note that Hibernate works on unmodified plain Java objects, performing dirty
032 * detection via copies made at load time. Returned objects can thus be used outside
033 * of an active Hibernate Session without any hassle, e.g. for display in a web GUI.
034 * Reassociating such instances with a new Session, e.g. for updates when coming
035 * back from the GUI, is straightforward, as the instance has kept its identity.
036 * You should care to reassociate them as early as possible though, to avoid having
037 * already loaded a version from the database in the same Session.
038 *
039 * @author Juergen Hoeller
040 * @since 1.2
041 * @see HibernateTemplate
042 * @see HibernateTransactionManager
043 * @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x
044 */
045@Deprecated
046public interface HibernateCallback<T> {
047
048        /**
049         * Gets called by {@code HibernateTemplate.execute} with an active
050         * Hibernate {@code Session}. Does not need to care about activating
051         * or closing the {@code Session}, or handling transactions.
052         * <p>If called without a thread-bound Hibernate transaction (initiated
053         * by HibernateTransactionManager), the code will simply get executed on the
054         * underlying JDBC connection with its transactional semantics. If Hibernate
055         * is configured to use a JTA-aware DataSource, the JDBC connection and thus
056         * the callback code will be transactional if a JTA transaction is active.
057         * <p>Allows for returning a result object created within the callback,
058         * i.e. a domain object or a collection of domain objects.
059         * A thrown custom RuntimeException is treated as an application exception:
060         * It gets propagated to the caller of the template.
061         * @param session active Hibernate session
062         * @return a result object, or {@code null} if none
063         * @throws HibernateException if thrown by the Hibernate API
064         * @throws SQLException if thrown by Hibernate-exposed JDBC API
065         * @see HibernateTemplate#execute
066         * @see HibernateTemplate#executeFind
067         */
068        T doInHibernate(Session session) throws HibernateException, SQLException;
069
070}