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.hibernate4;
018
019import org.hibernate.HibernateException;
020import org.hibernate.Session;
021
022/**
023 * Callback interface for Hibernate code. To be used with {@link HibernateTemplate}'s
024 * execution methods, often as anonymous classes within a method implementation.
025 * A typical implementation will call {@code Session.load/find/update} to perform
026 * some operations on persistent objects.
027 *
028 * @author Juergen Hoeller
029 * @since 4.0.1
030 * @see HibernateTemplate
031 * @see HibernateTransactionManager
032 */
033public interface HibernateCallback<T> {
034
035        /**
036         * Gets called by {@code HibernateTemplate.execute} with an active
037         * Hibernate {@code Session}. Does not need to care about activating
038         * or closing the {@code Session}, or handling transactions.
039         * <p>Allows for returning a result object created within the callback,
040         * i.e. a domain object or a collection of domain objects.
041         * A thrown custom RuntimeException is treated as an application exception:
042         * It gets propagated to the caller of the template.
043         * @param session active Hibernate session
044         * @return a result object, or {@code null} if none
045         * @throws HibernateException if thrown by the Hibernate API
046         * @see HibernateTemplate#execute
047         */
048        T doInHibernate(Session session) throws HibernateException;
049
050}