1 /* 2 * Copyright 2002-2012 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.springframework.orm; 18 19 import org.springframework.dao.DataRetrievalFailureException; 20 21 /** 22 * Exception thrown if a mapped object could not be retrieved via its identifier. 23 * Provides information about the persistent class and the identifier. 24 * 25 * @author Juergen Hoeller 26 * @since 13.10.2003 27 */ 28 @SuppressWarnings("serial") 29 public class ObjectRetrievalFailureException extends DataRetrievalFailureException { 30 31 private Object persistentClass; 32 33 private Object identifier; 34 35 36 /** 37 * Create a general ObjectRetrievalFailureException with the given message, 38 * without any information on the affected object. 39 * @param msg the detail message 40 * @param cause the source exception 41 */ 42 public ObjectRetrievalFailureException(String msg, Throwable cause) { 43 super(msg, cause); 44 } 45 46 /** 47 * Create a new ObjectRetrievalFailureException for the given object, 48 * with the default "not found" message. 49 * @param persistentClass the persistent class 50 * @param identifier the ID of the object that should have been retrieved 51 */ 52 public ObjectRetrievalFailureException(Class<?> persistentClass, Object identifier) { 53 this(persistentClass, identifier, 54 "Object of class [" + persistentClass.getName() + "] with identifier [" + identifier + "]: not found", 55 null); 56 } 57 58 /** 59 * Create a new ObjectRetrievalFailureException for the given object, 60 * with the given explicit message and exception. 61 * @param persistentClass the persistent class 62 * @param identifier the ID of the object that should have been retrieved 63 * @param msg the detail message 64 * @param cause the source exception 65 */ 66 public ObjectRetrievalFailureException( 67 Class<?> persistentClass, Object identifier, String msg, Throwable cause) { 68 69 super(msg, cause); 70 this.persistentClass = persistentClass; 71 this.identifier = identifier; 72 } 73 74 /** 75 * Create a new ObjectRetrievalFailureException for the given object, 76 * with the default "not found" message. 77 * @param persistentClassName the name of the persistent class 78 * @param identifier the ID of the object that should have been retrieved 79 */ 80 public ObjectRetrievalFailureException(String persistentClassName, Object identifier) { 81 this(persistentClassName, identifier, 82 "Object of class [" + persistentClassName + "] with identifier [" + identifier + "]: not found", 83 null); 84 } 85 86 /** 87 * Create a new ObjectRetrievalFailureException for the given object, 88 * with the given explicit message and exception. 89 * @param persistentClassName the name of the persistent class 90 * @param identifier the ID of the object that should have been retrieved 91 * @param msg the detail message 92 * @param cause the source exception 93 */ 94 public ObjectRetrievalFailureException( 95 String persistentClassName, Object identifier, String msg, Throwable cause) { 96 97 super(msg, cause); 98 this.persistentClass = persistentClassName; 99 this.identifier = identifier; 100 } 101 102 103 /** 104 * Return the persistent class of the object that was not found. 105 * If no Class was specified, this method returns null. 106 */ 107 public Class<?> getPersistentClass() { 108 return (this.persistentClass instanceof Class ? (Class<?>) this.persistentClass : null); 109 } 110 111 /** 112 * Return the name of the persistent class of the object that was not found. 113 * Will work for both Class objects and String names. 114 */ 115 public String getPersistentClassName() { 116 if (this.persistentClass instanceof Class) { 117 return ((Class<?>) this.persistentClass).getName(); 118 } 119 return (this.persistentClass != null ? this.persistentClass.toString() : null); 120 } 121 122 /** 123 * Return the identifier of the object that was not found. 124 */ 125 public Object getIdentifier() { 126 return identifier; 127 } 128 129 }