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;
018
019import org.springframework.dao.DataRetrievalFailureException;
020import org.springframework.lang.Nullable;
021
022/**
023 * Exception thrown if a mapped object could not be retrieved via its identifier.
024 * Provides information about the persistent class and the identifier.
025 *
026 * @author Juergen Hoeller
027 * @since 13.10.2003
028 */
029@SuppressWarnings("serial")
030public class ObjectRetrievalFailureException extends DataRetrievalFailureException {
031
032        @Nullable
033        private final Object persistentClass;
034
035        @Nullable
036        private final Object identifier;
037
038
039        /**
040         * Create a general ObjectRetrievalFailureException with the given message,
041         * without any information on the affected object.
042         * @param msg the detail message
043         * @param cause the source exception
044         */
045        public ObjectRetrievalFailureException(String msg, Throwable cause) {
046                super(msg, cause);
047                this.persistentClass = null;
048                this.identifier = null;
049        }
050
051        /**
052         * Create a new ObjectRetrievalFailureException for the given object,
053         * with the default "not found" message.
054         * @param persistentClass the persistent class
055         * @param identifier the ID of the object that should have been retrieved
056         */
057        public ObjectRetrievalFailureException(Class<?> persistentClass, Object identifier) {
058                this(persistentClass, identifier,
059                                "Object of class [" + persistentClass.getName() + "] with identifier [" + identifier + "]: not found",
060                                null);
061        }
062
063        /**
064         * Create a new ObjectRetrievalFailureException for the given object,
065         * with the given explicit message and exception.
066         * @param persistentClass the persistent class
067         * @param identifier the ID of the object that should have been retrieved
068         * @param msg the detail message
069         * @param cause the source exception
070         */
071        public ObjectRetrievalFailureException(
072                        Class<?> persistentClass, Object identifier, String msg, @Nullable Throwable cause) {
073
074                super(msg, cause);
075                this.persistentClass = persistentClass;
076                this.identifier = identifier;
077        }
078
079        /**
080         * Create a new ObjectRetrievalFailureException for the given object,
081         * with the default "not found" message.
082         * @param persistentClassName the name of the persistent class
083         * @param identifier the ID of the object that should have been retrieved
084         */
085        public ObjectRetrievalFailureException(String persistentClassName, Object identifier) {
086                this(persistentClassName, identifier,
087                                "Object of class [" + persistentClassName + "] with identifier [" + identifier + "]: not found",
088                                null);
089        }
090
091        /**
092         * Create a new ObjectRetrievalFailureException for the given object,
093         * with the given explicit message and exception.
094         * @param persistentClassName the name of the persistent class
095         * @param identifier the ID of the object that should have been retrieved
096         * @param msg the detail message
097         * @param cause the source exception
098         */
099        public ObjectRetrievalFailureException(
100                        String persistentClassName, Object identifier, String msg, @Nullable Throwable cause) {
101
102                super(msg, cause);
103                this.persistentClass = persistentClassName;
104                this.identifier = identifier;
105        }
106
107
108        /**
109         * Return the persistent class of the object that was not found.
110         * If no Class was specified, this method returns null.
111         */
112        @Nullable
113        public Class<?> getPersistentClass() {
114                return (this.persistentClass instanceof Class ? (Class<?>) this.persistentClass : null);
115        }
116
117        /**
118         * Return the name of the persistent class of the object that was not found.
119         * Will work for both Class objects and String names.
120         */
121        @Nullable
122        public String getPersistentClassName() {
123                if (this.persistentClass instanceof Class) {
124                        return ((Class<?>) this.persistentClass).getName();
125                }
126                return (this.persistentClass != null ? this.persistentClass.toString() : null);
127        }
128
129        /**
130         * Return the identifier of the object that was not found.
131         */
132        @Nullable
133        public Object getIdentifier() {
134                return this.identifier;
135        }
136
137}