001/*
002 * Copyright 2002-2018 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.hibernate5;
018
019import org.hibernate.HibernateException;
020import org.hibernate.Session;
021
022import org.springframework.lang.Nullable;
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.
029 *
030 * @author Juergen Hoeller
031 * @since 4.2
032 * @param <T> the result type
033 * @see HibernateTemplate
034 * @see HibernateTransactionManager
035 */
036@FunctionalInterface
037public interface HibernateCallback<T> {
038
039        /**
040         * Gets called by {@code HibernateTemplate.execute} with an active
041         * Hibernate {@code Session}. Does not need to care about activating
042         * or closing the {@code Session}, or handling transactions.
043         * <p>Allows for returning a result object created within the callback,
044         * i.e. a domain object or a collection of domain objects.
045         * A thrown custom RuntimeException is treated as an application exception:
046         * It gets propagated to the caller of the template.
047         * @param session active Hibernate session
048         * @return a result object, or {@code null} if none
049         * @throws HibernateException if thrown by the Hibernate API
050         * @see HibernateTemplate#execute
051         */
052        @Nullable
053        T doInHibernate(Session session) throws HibernateException;
054
055}