001/* 002 * Copyright 2002-2012 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.dao; 018 019/** 020 * Data access exception thrown when a result was not of the expected size, 021 * for example when expecting a single row but getting 0 or more than 1 rows. 022 * 023 * @author Juergen Hoeller 024 * @author Chris Beams 025 * @since 1.0.2 026 * @see EmptyResultDataAccessException 027 */ 028@SuppressWarnings("serial") 029public class IncorrectResultSizeDataAccessException extends DataRetrievalFailureException { 030 031 private int expectedSize; 032 033 private int actualSize; 034 035 036 /** 037 * Constructor for IncorrectResultSizeDataAccessException. 038 * @param expectedSize the expected result size 039 */ 040 public IncorrectResultSizeDataAccessException(int expectedSize) { 041 super("Incorrect result size: expected " + expectedSize); 042 this.expectedSize = expectedSize; 043 this.actualSize = -1; 044 } 045 046 /** 047 * Constructor for IncorrectResultSizeDataAccessException. 048 * @param expectedSize the expected result size 049 * @param actualSize the actual result size (or -1 if unknown) 050 */ 051 public IncorrectResultSizeDataAccessException(int expectedSize, int actualSize) { 052 super("Incorrect result size: expected " + expectedSize + ", actual " + actualSize); 053 this.expectedSize = expectedSize; 054 this.actualSize = actualSize; 055 } 056 057 /** 058 * Constructor for IncorrectResultSizeDataAccessException. 059 * @param msg the detail message 060 * @param expectedSize the expected result size 061 */ 062 public IncorrectResultSizeDataAccessException(String msg, int expectedSize) { 063 super(msg); 064 this.expectedSize = expectedSize; 065 this.actualSize = -1; 066 } 067 068 /** 069 * Constructor for IncorrectResultSizeDataAccessException. 070 * @param msg the detail message 071 * @param expectedSize the expected result size 072 * @param ex the wrapped exception 073 */ 074 public IncorrectResultSizeDataAccessException(String msg, int expectedSize, Throwable ex) { 075 super(msg, ex); 076 this.expectedSize = expectedSize; 077 this.actualSize = -1; 078 } 079 080 /** 081 * Constructor for IncorrectResultSizeDataAccessException. 082 * @param msg the detail message 083 * @param expectedSize the expected result size 084 * @param actualSize the actual result size (or -1 if unknown) 085 */ 086 public IncorrectResultSizeDataAccessException(String msg, int expectedSize, int actualSize) { 087 super(msg); 088 this.expectedSize = expectedSize; 089 this.actualSize = actualSize; 090 } 091 092 /** 093 * Constructor for IncorrectResultSizeDataAccessException. 094 * @param msg the detail message 095 * @param expectedSize the expected result size 096 * @param actualSize the actual result size (or -1 if unknown) 097 * @param ex the wrapped exception 098 */ 099 public IncorrectResultSizeDataAccessException(String msg, int expectedSize, int actualSize, Throwable ex) { 100 super(msg, ex); 101 this.expectedSize = expectedSize; 102 this.actualSize = actualSize; 103 } 104 105 106 /** 107 * Return the expected result size. 108 */ 109 public int getExpectedSize() { 110 return this.expectedSize; 111 } 112 113 /** 114 * Return the actual result size (or -1 if unknown). 115 */ 116 public int getActualSize() { 117 return this.actualSize; 118 } 119 120}