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