0
# Data Access Exception Translation
1
2
Spring's DAO exception hierarchy provides consistent error handling across different data access technologies.
3
4
## Core Exception Hierarchy
5
6
```java { .api }
7
public abstract class DataAccessException extends NestedRuntimeException {
8
public DataAccessException(String msg);
9
public DataAccessException(String msg, @Nullable Throwable cause);
10
}
11
12
// Key exception types
13
public class DataIntegrityViolationException extends NonTransientDataAccessException {}
14
public class DataRetrievalFailureException extends NonTransientDataAccessException {}
15
public class DeadlockLoserDataAccessException extends PessimisticLockingFailureException {}
16
public class DuplicateKeyException extends DataIntegrityViolationException {}
17
public class OptimisticLockingFailureException extends ConcurrencyFailureException {}
18
public class PessimisticLockingFailureException extends ConcurrencyFailureException {}
19
public class QueryTimeoutException extends TransientDataAccessException {}
20
```
21
22
## DataAccessUtils
23
24
```java { .api }
25
public abstract class DataAccessUtils {
26
public static <T> T singleResult(@Nullable Collection<T> results)
27
throws IncorrectResultSizeDataAccessException;
28
public static <T> T requiredSingleResult(@Nullable Collection<T> results)
29
throws IncorrectResultSizeDataAccessException;
30
public static <T> T uniqueResult(@Nullable Collection<T> results)
31
throws IncorrectResultSizeDataAccessException;
32
}
33
```
34
35
## Usage Examples
36
37
```java
38
@Service
39
public class UserService {
40
41
public User createUser(User user) {
42
try {
43
return userRepository.save(user);
44
} catch (DuplicateKeyException e) {
45
throw new UserAlreadyExistsException(user.getEmail());
46
} catch (DataIntegrityViolationException e) {
47
throw new InvalidUserDataException(e.getMessage());
48
}
49
}
50
}
51
```