1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -253,14 +253,18 @@ public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
253
253
* @return the corresponding DataAccessException instance
254
254
*/
255
255
protected DataAccessException convertHibernateAccessException (HibernateException ex ) {
256
- if (this .jdbcExceptionTranslator != null && ex instanceof JDBCException jdbcEx ) {
256
+ return convertHibernateAccessException (ex , ex );
257
+ }
258
+
259
+ private DataAccessException convertHibernateAccessException (HibernateException ex , HibernateException exToCheck ) {
260
+ if (this .jdbcExceptionTranslator != null && exToCheck instanceof JDBCException jdbcEx ) {
257
261
DataAccessException dae = this .jdbcExceptionTranslator .translate (
258
262
"Hibernate operation: " + jdbcEx .getMessage (), jdbcEx .getSQL (), jdbcEx .getSQLException ());
259
263
if (dae != null ) {
260
264
return dae ;
261
265
}
262
266
}
263
- if (this .transactionExceptionTranslator != null && ex instanceof org .hibernate .TransactionException ) {
267
+ if (this .transactionExceptionTranslator != null && exToCheck instanceof org .hibernate .TransactionException ) {
264
268
if (ex .getCause () instanceof SQLException sqlEx ) {
265
269
DataAccessException dae = this .transactionExceptionTranslator .translate (
266
270
"Hibernate transaction: " + ex .getMessage (), null , sqlEx );
@@ -270,74 +274,77 @@ protected DataAccessException convertHibernateAccessException(HibernateException
270
274
}
271
275
}
272
276
273
- if (ex instanceof JDBCConnectionException ) {
277
+ if (exToCheck instanceof JDBCConnectionException ) {
274
278
return new DataAccessResourceFailureException (ex .getMessage (), ex );
275
279
}
276
- if (ex instanceof SQLGrammarException hibEx ) {
280
+ if (exToCheck instanceof SQLGrammarException hibEx ) {
277
281
return new InvalidDataAccessResourceUsageException (ex .getMessage () + "; SQL [" + hibEx .getSQL () + "]" , ex );
278
282
}
279
- if (ex instanceof QueryTimeoutException hibEx ) {
283
+ if (exToCheck instanceof QueryTimeoutException hibEx ) {
280
284
return new org .springframework .dao .QueryTimeoutException (ex .getMessage () + "; SQL [" + hibEx .getSQL () + "]" , ex );
281
285
}
282
- if (ex instanceof LockAcquisitionException hibEx ) {
286
+ if (exToCheck instanceof LockAcquisitionException hibEx ) {
283
287
return new CannotAcquireLockException (ex .getMessage () + "; SQL [" + hibEx .getSQL () + "]" , ex );
284
288
}
285
- if (ex instanceof PessimisticLockException hibEx ) {
289
+ if (exToCheck instanceof PessimisticLockException hibEx ) {
286
290
return new PessimisticLockingFailureException (ex .getMessage () + "; SQL [" + hibEx .getSQL () + "]" , ex );
287
291
}
288
- if (ex instanceof ConstraintViolationException hibEx ) {
292
+ if (exToCheck instanceof ConstraintViolationException hibEx ) {
289
293
return new DataIntegrityViolationException (ex .getMessage () + "; SQL [" + hibEx .getSQL () +
290
294
"]; constraint [" + hibEx .getConstraintName () + "]" , ex );
291
295
}
292
- if (ex instanceof DataException hibEx ) {
296
+ if (exToCheck instanceof DataException hibEx ) {
293
297
return new DataIntegrityViolationException (ex .getMessage () + "; SQL [" + hibEx .getSQL () + "]" , ex );
294
298
}
295
299
// end of JDBCException subclass handling
296
300
297
- if (ex instanceof QueryException ) {
301
+ if (exToCheck instanceof QueryException ) {
298
302
return new InvalidDataAccessResourceUsageException (ex .getMessage (), ex );
299
303
}
300
- if (ex instanceof NonUniqueResultException ) {
304
+ if (exToCheck instanceof NonUniqueResultException ) {
301
305
return new IncorrectResultSizeDataAccessException (ex .getMessage (), 1 , ex );
302
306
}
303
- if (ex instanceof NonUniqueObjectException ) {
307
+ if (exToCheck instanceof NonUniqueObjectException ) {
304
308
return new DuplicateKeyException (ex .getMessage (), ex );
305
309
}
306
- if (ex instanceof PropertyValueException ) {
310
+ if (exToCheck instanceof PropertyValueException ) {
307
311
return new DataIntegrityViolationException (ex .getMessage (), ex );
308
312
}
309
- if (ex instanceof PersistentObjectException ) {
313
+ if (exToCheck instanceof PersistentObjectException ) {
310
314
return new InvalidDataAccessApiUsageException (ex .getMessage (), ex );
311
315
}
312
- if (ex instanceof TransientObjectException ) {
316
+ if (exToCheck instanceof TransientObjectException ) {
313
317
return new InvalidDataAccessApiUsageException (ex .getMessage (), ex );
314
318
}
315
- if (ex instanceof ObjectDeletedException ) {
319
+ if (exToCheck instanceof ObjectDeletedException ) {
316
320
return new InvalidDataAccessApiUsageException (ex .getMessage (), ex );
317
321
}
318
- if (ex instanceof UnresolvableObjectException hibEx ) {
322
+ if (exToCheck instanceof UnresolvableObjectException hibEx ) {
319
323
return new ObjectRetrievalFailureException (hibEx .getEntityName (), getIdentifier (hibEx ), ex .getMessage (), ex );
320
324
}
321
- if (ex instanceof WrongClassException hibEx ) {
325
+ if (exToCheck instanceof WrongClassException hibEx ) {
322
326
return new ObjectRetrievalFailureException (hibEx .getEntityName (), getIdentifier (hibEx ), ex .getMessage (), ex );
323
327
}
324
- if (ex instanceof StaleObjectStateException hibEx ) {
328
+ if (exToCheck instanceof StaleObjectStateException hibEx ) {
325
329
return new ObjectOptimisticLockingFailureException (hibEx .getEntityName (), getIdentifier (hibEx ), ex .getMessage (), ex );
326
330
}
327
- if (ex instanceof StaleStateException ) {
331
+ if (exToCheck instanceof StaleStateException ) {
328
332
return new ObjectOptimisticLockingFailureException (ex .getMessage (), ex );
329
333
}
330
- if (ex instanceof OptimisticEntityLockException ) {
334
+ if (exToCheck instanceof OptimisticEntityLockException ) {
331
335
return new ObjectOptimisticLockingFailureException (ex .getMessage (), ex );
332
336
}
333
- if (ex instanceof PessimisticEntityLockException ) {
337
+ if (exToCheck instanceof PessimisticEntityLockException ) {
334
338
if (ex .getCause () instanceof LockAcquisitionException ) {
335
339
return new CannotAcquireLockException (ex .getMessage (), ex .getCause ());
336
340
}
337
341
return new PessimisticLockingFailureException (ex .getMessage (), ex );
338
342
}
339
343
340
- // fallback
344
+ // Fallback: check potentially more specific cause, otherwise JpaSystemException
345
+ if (exToCheck .getCause () instanceof HibernateException causeToCheck ) {
346
+ return convertHibernateAccessException (ex , causeToCheck );
347
+ }
341
348
return new JpaSystemException (ex );
342
349
}
343
350
0 commit comments