11/*
2- * Copyright 2002-2023 the original author or authors.
2+ * Copyright 2002-2025 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -253,14 +253,18 @@ public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
253253 * @return the corresponding DataAccessException instance
254254 */
255255 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 ) {
257261 DataAccessException dae = this .jdbcExceptionTranslator .translate (
258262 "Hibernate operation: " + jdbcEx .getMessage (), jdbcEx .getSQL (), jdbcEx .getSQLException ());
259263 if (dae != null ) {
260264 return dae ;
261265 }
262266 }
263- if (this .transactionExceptionTranslator != null && ex instanceof org .hibernate .TransactionException ) {
267+ if (this .transactionExceptionTranslator != null && exToCheck instanceof org .hibernate .TransactionException ) {
264268 if (ex .getCause () instanceof SQLException sqlEx ) {
265269 DataAccessException dae = this .transactionExceptionTranslator .translate (
266270 "Hibernate transaction: " + ex .getMessage (), null , sqlEx );
@@ -270,74 +274,77 @@ protected DataAccessException convertHibernateAccessException(HibernateException
270274 }
271275 }
272276
273- if (ex instanceof JDBCConnectionException ) {
277+ if (exToCheck instanceof JDBCConnectionException ) {
274278 return new DataAccessResourceFailureException (ex .getMessage (), ex );
275279 }
276- if (ex instanceof SQLGrammarException hibEx ) {
280+ if (exToCheck instanceof SQLGrammarException hibEx ) {
277281 return new InvalidDataAccessResourceUsageException (ex .getMessage () + "; SQL [" + hibEx .getSQL () + "]" , ex );
278282 }
279- if (ex instanceof QueryTimeoutException hibEx ) {
283+ if (exToCheck instanceof QueryTimeoutException hibEx ) {
280284 return new org .springframework .dao .QueryTimeoutException (ex .getMessage () + "; SQL [" + hibEx .getSQL () + "]" , ex );
281285 }
282- if (ex instanceof LockAcquisitionException hibEx ) {
286+ if (exToCheck instanceof LockAcquisitionException hibEx ) {
283287 return new CannotAcquireLockException (ex .getMessage () + "; SQL [" + hibEx .getSQL () + "]" , ex );
284288 }
285- if (ex instanceof PessimisticLockException hibEx ) {
289+ if (exToCheck instanceof PessimisticLockException hibEx ) {
286290 return new PessimisticLockingFailureException (ex .getMessage () + "; SQL [" + hibEx .getSQL () + "]" , ex );
287291 }
288- if (ex instanceof ConstraintViolationException hibEx ) {
292+ if (exToCheck instanceof ConstraintViolationException hibEx ) {
289293 return new DataIntegrityViolationException (ex .getMessage () + "; SQL [" + hibEx .getSQL () +
290294 "]; constraint [" + hibEx .getConstraintName () + "]" , ex );
291295 }
292- if (ex instanceof DataException hibEx ) {
296+ if (exToCheck instanceof DataException hibEx ) {
293297 return new DataIntegrityViolationException (ex .getMessage () + "; SQL [" + hibEx .getSQL () + "]" , ex );
294298 }
295299 // end of JDBCException subclass handling
296300
297- if (ex instanceof QueryException ) {
301+ if (exToCheck instanceof QueryException ) {
298302 return new InvalidDataAccessResourceUsageException (ex .getMessage (), ex );
299303 }
300- if (ex instanceof NonUniqueResultException ) {
304+ if (exToCheck instanceof NonUniqueResultException ) {
301305 return new IncorrectResultSizeDataAccessException (ex .getMessage (), 1 , ex );
302306 }
303- if (ex instanceof NonUniqueObjectException ) {
307+ if (exToCheck instanceof NonUniqueObjectException ) {
304308 return new DuplicateKeyException (ex .getMessage (), ex );
305309 }
306- if (ex instanceof PropertyValueException ) {
310+ if (exToCheck instanceof PropertyValueException ) {
307311 return new DataIntegrityViolationException (ex .getMessage (), ex );
308312 }
309- if (ex instanceof PersistentObjectException ) {
313+ if (exToCheck instanceof PersistentObjectException ) {
310314 return new InvalidDataAccessApiUsageException (ex .getMessage (), ex );
311315 }
312- if (ex instanceof TransientObjectException ) {
316+ if (exToCheck instanceof TransientObjectException ) {
313317 return new InvalidDataAccessApiUsageException (ex .getMessage (), ex );
314318 }
315- if (ex instanceof ObjectDeletedException ) {
319+ if (exToCheck instanceof ObjectDeletedException ) {
316320 return new InvalidDataAccessApiUsageException (ex .getMessage (), ex );
317321 }
318- if (ex instanceof UnresolvableObjectException hibEx ) {
322+ if (exToCheck instanceof UnresolvableObjectException hibEx ) {
319323 return new ObjectRetrievalFailureException (hibEx .getEntityName (), getIdentifier (hibEx ), ex .getMessage (), ex );
320324 }
321- if (ex instanceof WrongClassException hibEx ) {
325+ if (exToCheck instanceof WrongClassException hibEx ) {
322326 return new ObjectRetrievalFailureException (hibEx .getEntityName (), getIdentifier (hibEx ), ex .getMessage (), ex );
323327 }
324- if (ex instanceof StaleObjectStateException hibEx ) {
328+ if (exToCheck instanceof StaleObjectStateException hibEx ) {
325329 return new ObjectOptimisticLockingFailureException (hibEx .getEntityName (), getIdentifier (hibEx ), ex .getMessage (), ex );
326330 }
327- if (ex instanceof StaleStateException ) {
331+ if (exToCheck instanceof StaleStateException ) {
328332 return new ObjectOptimisticLockingFailureException (ex .getMessage (), ex );
329333 }
330- if (ex instanceof OptimisticEntityLockException ) {
334+ if (exToCheck instanceof OptimisticEntityLockException ) {
331335 return new ObjectOptimisticLockingFailureException (ex .getMessage (), ex );
332336 }
333- if (ex instanceof PessimisticEntityLockException ) {
337+ if (exToCheck instanceof PessimisticEntityLockException ) {
334338 if (ex .getCause () instanceof LockAcquisitionException ) {
335339 return new CannotAcquireLockException (ex .getMessage (), ex .getCause ());
336340 }
337341 return new PessimisticLockingFailureException (ex .getMessage (), ex );
338342 }
339343
340- // fallback
344+ // Fallback: check potentially more specific cause, otherwise JpaSystemException
345+ if (exToCheck .getCause () instanceof HibernateException causeToCheck ) {
346+ return convertHibernateAccessException (ex , causeToCheck );
347+ }
341348 return new JpaSystemException (ex );
342349 }
343350
0 commit comments