diff --git a/spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateExceptionTranslator.java b/spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateExceptionTranslator.java index 2f69163421..6b66360c22 100644 --- a/spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateExceptionTranslator.java +++ b/spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateExceptionTranslator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -90,8 +90,7 @@ public class HibernateExceptionTranslator implements PersistenceExceptionTransla * @see SessionFactoryUtils#convertHibernateAccessException */ protected DataAccessException convertHibernateAccessException(HibernateException ex) { - if (this.jdbcExceptionTranslator != null && ex instanceof JDBCException) { - JDBCException jdbcEx = (JDBCException) ex; + if (this.jdbcExceptionTranslator != null && ex instanceof JDBCException jdbcEx) { DataAccessException dae = this.jdbcExceptionTranslator.translate( "Hibernate operation: " + jdbcEx.getMessage(), jdbcEx.getSQL(), jdbcEx.getSQLException()); if (dae != null) { diff --git a/spring-orm/src/main/java/org/springframework/orm/hibernate5/SessionFactoryUtils.java b/spring-orm/src/main/java/org/springframework/orm/hibernate5/SessionFactoryUtils.java index 32cf2bc90e..ce92d686ce 100644 --- a/spring-orm/src/main/java/org/springframework/orm/hibernate5/SessionFactoryUtils.java +++ b/spring-orm/src/main/java/org/springframework/orm/hibernate5/SessionFactoryUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -159,8 +159,7 @@ public abstract class SessionFactoryUtils { } } } - if (sessionFactory instanceof SessionFactoryImplementor) { - SessionFactoryImplementor sfi = (SessionFactoryImplementor) sessionFactory; + if (sessionFactory instanceof SessionFactoryImplementor sfi) { try { ConnectionProvider cp = sfi.getServiceRegistry().getService(ConnectionProvider.class); if (cp != null) { @@ -188,29 +187,23 @@ public abstract class SessionFactoryUtils { if (ex instanceof JDBCConnectionException) { return new DataAccessResourceFailureException(ex.getMessage(), ex); } - if (ex instanceof SQLGrammarException) { - SQLGrammarException jdbcEx = (SQLGrammarException) ex; + if (ex instanceof SQLGrammarException jdbcEx) { return new InvalidDataAccessResourceUsageException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } - if (ex instanceof QueryTimeoutException) { - QueryTimeoutException jdbcEx = (QueryTimeoutException) ex; + if (ex instanceof QueryTimeoutException jdbcEx) { return new org.springframework.dao.QueryTimeoutException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } - if (ex instanceof LockAcquisitionException) { - LockAcquisitionException jdbcEx = (LockAcquisitionException) ex; + if (ex instanceof LockAcquisitionException jdbcEx) { return new CannotAcquireLockException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } - if (ex instanceof PessimisticLockException) { - PessimisticLockException jdbcEx = (PessimisticLockException) ex; + if (ex instanceof PessimisticLockException jdbcEx) { return new PessimisticLockingFailureException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } - if (ex instanceof ConstraintViolationException) { - ConstraintViolationException jdbcEx = (ConstraintViolationException) ex; + if (ex instanceof ConstraintViolationException jdbcEx) { return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]; constraint [" + jdbcEx.getConstraintName() + "]", ex); } - if (ex instanceof DataException) { - DataException jdbcEx = (DataException) ex; + if (ex instanceof DataException jdbcEx) { return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof JDBCException) { diff --git a/spring-orm/src/main/java/org/springframework/orm/hibernate5/SpringSessionContext.java b/spring-orm/src/main/java/org/springframework/orm/hibernate5/SpringSessionContext.java index a7a5e2f3d2..ea814641ca 100644 --- a/spring-orm/src/main/java/org/springframework/orm/hibernate5/SpringSessionContext.java +++ b/spring-orm/src/main/java/org/springframework/orm/hibernate5/SpringSessionContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -81,12 +81,11 @@ public class SpringSessionContext implements CurrentSessionContext { @Override public Session currentSession() throws HibernateException { Object value = TransactionSynchronizationManager.getResource(this.sessionFactory); - if (value instanceof Session) { - return (Session) value; + if (value instanceof Session session) { + return session; } - else if (value instanceof SessionHolder) { + else if (value instanceof SessionHolder sessionHolder) { // HibernateTransactionManager - SessionHolder sessionHolder = (SessionHolder) value; Session session = sessionHolder.getSession(); if (!sessionHolder.isSynchronizedWithTransaction() && TransactionSynchronizationManager.isSynchronizationActive()) { @@ -104,9 +103,9 @@ public class SpringSessionContext implements CurrentSessionContext { } return session; } - else if (value instanceof EntityManagerHolder) { + else if (value instanceof EntityManagerHolder entityManagerHolder) { // JpaTransactionManager - return ((EntityManagerHolder) value).getEntityManager().unwrap(Session.class); + return entityManagerHolder.getEntityManager().unwrap(Session.class); } if (this.transactionManager != null && this.jtaSessionContext != null) { diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/AbstractEntityManagerFactoryBean.java b/spring-orm/src/main/java/org/springframework/orm/jpa/AbstractEntityManagerFactoryBean.java index 1a73282546..1c85df6d06 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/AbstractEntityManagerFactoryBean.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/AbstractEntityManagerFactoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -501,12 +501,12 @@ public abstract class AbstractEntityManagerFactoryBean implements if (args != null) { for (int i = 0; i < args.length; i++) { Object arg = args[i]; - if (arg instanceof Query && Proxy.isProxyClass(arg.getClass())) { + if (arg instanceof Query query && Proxy.isProxyClass(arg.getClass())) { // Assumably a Spring-generated proxy from SharedEntityManagerCreator: // since we're passing it back to the native EntityManagerFactory, // let's unwrap it to the original Query object from the provider. try { - args[i] = ((Query) arg).unwrap(null); + args[i] = query.unwrap(null); } catch (RuntimeException ex) { // Ignore - simply proceed with given Query object then @@ -517,9 +517,8 @@ public abstract class AbstractEntityManagerFactoryBean implements // Standard delegation to the native factory, just post-processing EntityManager return values Object retVal = method.invoke(getNativeEntityManagerFactory(), args); - if (retVal instanceof EntityManager) { + if (retVal instanceof EntityManager rawEntityManager) { // Any other createEntityManager variant - expecting non-synchronized semantics - EntityManager rawEntityManager = (EntityManager) retVal; postProcessEntityManager(rawEntityManager); retVal = ExtendedEntityManagerCreator.createApplicationManagedEntityManager(rawEntityManager, this, false); } @@ -568,9 +567,9 @@ public abstract class AbstractEntityManagerFactoryBean implements } catch (ExecutionException ex) { Throwable cause = ex.getCause(); - if (cause instanceof PersistenceException) { + if (cause instanceof PersistenceException persistenceException) { // Rethrow a provider configuration exception (possibly with a nested cause) directly - throw (PersistenceException) cause; + throw persistenceException; } throw new IllegalStateException("Failed to asynchronously initialize native EntityManagerFactory: " + ex.getMessage(), cause); diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/EntityManagerFactoryAccessor.java b/spring-orm/src/main/java/org/springframework/orm/jpa/EntityManagerFactoryAccessor.java index 5f73add858..045559a02f 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/EntityManagerFactoryAccessor.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/EntityManagerFactoryAccessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -146,11 +146,10 @@ public abstract class EntityManagerFactoryAccessor implements BeanFactoryAware { @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { if (getEntityManagerFactory() == null) { - if (!(beanFactory instanceof ListableBeanFactory)) { + if (!(beanFactory instanceof ListableBeanFactory lbf)) { throw new IllegalStateException("Cannot retrieve EntityManagerFactory by persistence unit name " + "in a non-listable BeanFactory: " + beanFactory); } - ListableBeanFactory lbf = (ListableBeanFactory) beanFactory; setEntityManagerFactory(EntityManagerFactoryUtils.findEntityManagerFactory(lbf, getPersistenceUnitName())); } } diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/EntityManagerFactoryUtils.java b/spring-orm/src/main/java/org/springframework/orm/jpa/EntityManagerFactoryUtils.java index ceb470a9ea..d8e515c163 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/EntityManagerFactoryUtils.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/EntityManagerFactoryUtils.java @@ -302,8 +302,7 @@ public abstract class EntityManagerFactoryUtils { */ @Nullable private static Object prepareTransaction(EntityManager em, EntityManagerFactory emf) { - if (emf instanceof EntityManagerFactoryInfo) { - EntityManagerFactoryInfo emfInfo = (EntityManagerFactoryInfo) emf; + if (emf instanceof EntityManagerFactoryInfo emfInfo) { JpaDialect jpaDialect = emfInfo.getJpaDialect(); if (jpaDialect != null) { return jpaDialect.prepareTransaction(em, @@ -322,8 +321,7 @@ public abstract class EntityManagerFactoryUtils { * @see JpaDialect#cleanupTransaction */ private static void cleanupTransaction(@Nullable Object transactionData, EntityManagerFactory emf) { - if (emf instanceof EntityManagerFactoryInfo) { - EntityManagerFactoryInfo emfInfo = (EntityManagerFactoryInfo) emf; + if (emf instanceof EntityManagerFactoryInfo emfInfo) { JpaDialect jpaDialect = emfInfo.getJpaDialect(); if (jpaDialect != null) { jpaDialect.cleanupTransaction(transactionData); diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/ExtendedEntityManagerCreator.java b/spring-orm/src/main/java/org/springframework/orm/jpa/ExtendedEntityManagerCreator.java index 29b7a008ca..452eb81b96 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/ExtendedEntityManagerCreator.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/ExtendedEntityManagerCreator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -169,8 +169,7 @@ public abstract class ExtendedEntityManagerCreator { EntityManagerFactory emf, @Nullable Map properties, boolean synchronizedWithTransaction) { Assert.notNull(emf, "EntityManagerFactory must not be null"); - if (emf instanceof EntityManagerFactoryInfo) { - EntityManagerFactoryInfo emfInfo = (EntityManagerFactoryInfo) emf; + if (emf instanceof EntityManagerFactoryInfo emfInfo) { EntityManager rawEntityManager = emfInfo.createNativeEntityManager(properties); return createProxy(rawEntityManager, emfInfo, true, synchronizedWithTransaction); } diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/JpaTransactionManager.java b/spring-orm/src/main/java/org/springframework/orm/jpa/JpaTransactionManager.java index 79e48b7d0f..3c02cc2d11 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/JpaTransactionManager.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/JpaTransactionManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -326,11 +326,10 @@ public class JpaTransactionManager extends AbstractPlatformTransactionManager @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { if (getEntityManagerFactory() == null) { - if (!(beanFactory instanceof ListableBeanFactory)) { + if (!(beanFactory instanceof ListableBeanFactory lbf)) { throw new IllegalStateException("Cannot retrieve EntityManagerFactory by persistence unit name " + "in a non-listable BeanFactory: " + beanFactory); } - ListableBeanFactory lbf = (ListableBeanFactory) beanFactory; setEntityManagerFactory(EntityManagerFactoryUtils.findEntityManagerFactory(lbf, getPersistenceUnitName())); } } diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/SharedEntityManagerCreator.java b/spring-orm/src/main/java/org/springframework/orm/jpa/SharedEntityManagerCreator.java index 26add517b0..0fb6acc3ba 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/SharedEntityManagerCreator.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/SharedEntityManagerCreator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -308,8 +308,7 @@ public abstract class SharedEntityManagerCreator { // Invoke method on current EntityManager. try { Object result = method.invoke(target, args); - if (result instanceof Query) { - Query query = (Query) result; + if (result instanceof Query query) { if (isNewEm) { Class[] ifcs = cachedQueryInterfaces.computeIfAbsent(query.getClass(), key -> ClassUtils.getAllInterfacesForClass(key, this.proxyClassLoader)); @@ -419,8 +418,7 @@ public abstract class SharedEntityManagerCreator { if (queryTerminatingMethods.contains(method.getName())) { // Actual execution of the query: close the EntityManager right // afterwards, since that was the only reason we kept it open. - if (this.outputParameters != null && this.target instanceof StoredProcedureQuery) { - StoredProcedureQuery storedProc = (StoredProcedureQuery) this.target; + if (this.outputParameters != null && this.target instanceof StoredProcedureQuery storedProc) { for (Map.Entry entry : this.outputParameters.entrySet()) { try { Object key = entry.getKey(); diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java b/spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java index 20747130ac..3f8171c84b 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -324,8 +324,8 @@ public class PersistenceAnnotationBeanPostProcessor @Override public void setBeanFactory(BeanFactory beanFactory) { - if (beanFactory instanceof ListableBeanFactory) { - this.beanFactory = (ListableBeanFactory) beanFactory; + if (beanFactory instanceof ListableBeanFactory lbf) { + this.beanFactory = lbf; } } @@ -543,8 +543,8 @@ public class PersistenceAnnotationBeanPostProcessor Assert.state(this.beanFactory != null, "ListableBeanFactory required for EntityManagerFactory bean lookup"); EntityManagerFactory emf = EntityManagerFactoryUtils.findEntityManagerFactory(this.beanFactory, unitName); - if (requestingBeanName != null && this.beanFactory instanceof ConfigurableBeanFactory) { - ((ConfigurableBeanFactory) this.beanFactory).registerDependentBean(unitName, requestingBeanName); + if (requestingBeanName != null && this.beanFactory instanceof ConfigurableBeanFactory cbf) { + cbf.registerDependentBean(unitName, requestingBeanName); } return emf; } @@ -559,9 +559,8 @@ public class PersistenceAnnotationBeanPostProcessor Assert.state(this.beanFactory != null, "ListableBeanFactory required for EntityManagerFactory bean lookup"); - if (this.beanFactory instanceof ConfigurableListableBeanFactory) { + if (this.beanFactory instanceof ConfigurableListableBeanFactory clbf) { // Fancy variant with dependency registration - ConfigurableListableBeanFactory clbf = (ConfigurableListableBeanFactory) this.beanFactory; NamedBeanHolder emfHolder = clbf.resolveNamedBean(EntityManagerFactory.class); if (requestingBeanName != null) { clbf.registerDependentBean(emfHolder.getBeanName(), requestingBeanName); @@ -596,11 +595,11 @@ public class PersistenceAnnotationBeanPostProcessor public T lookup(String jndiName, Class requiredType) throws Exception { JndiLocatorDelegate locator = new JndiLocatorDelegate(); - if (jndiEnvironment instanceof JndiTemplate) { - locator.setJndiTemplate((JndiTemplate) jndiEnvironment); + if (jndiEnvironment instanceof JndiTemplate jndiTemplate) { + locator.setJndiTemplate(jndiTemplate); } - else if (jndiEnvironment instanceof Properties) { - locator.setJndiEnvironment((Properties) jndiEnvironment); + else if (jndiEnvironment instanceof Properties properties) { + locator.setJndiEnvironment(properties); } else if (jndiEnvironment != null) { throw new IllegalStateException("Illegal 'jndiEnvironment' type: " + jndiEnvironment.getClass()); @@ -696,8 +695,7 @@ public class PersistenceAnnotationBeanPostProcessor emf = findEntityManagerFactory(this.unitName, requestingBeanName); } // Inject a shared transactional EntityManager proxy. - if (emf instanceof EntityManagerFactoryInfo && - ((EntityManagerFactoryInfo) emf).getEntityManagerInterface() != null) { + if (emf instanceof EntityManagerFactoryInfo emfInfo && emfInfo.getEntityManagerInterface() != null) { // Create EntityManager based on the info's vendor-specific type // (which might be more specific than the field's type). em = SharedEntityManagerCreator.createSharedEntityManager( @@ -727,9 +725,9 @@ public class PersistenceAnnotationBeanPostProcessor em = ExtendedEntityManagerCreator.createContainerManagedEntityManager( emf, this.properties, this.synchronizedWithTransaction); } - if (em instanceof EntityManagerProxy && beanFactory != null && requestingBeanName != null && + if (em instanceof EntityManagerProxy emp && beanFactory != null && requestingBeanName != null && beanFactory.containsBean(requestingBeanName) && !beanFactory.isPrototype(requestingBeanName)) { - extendedEntityManagersToClose.put(target, ((EntityManagerProxy) em).getTargetEntityManager()); + extendedEntityManagersToClose.put(target, emp.getTargetEntityManager()); } return em; } diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/support/SharedEntityManagerBean.java b/spring-orm/src/main/java/org/springframework/orm/jpa/support/SharedEntityManagerBean.java index a3f465dc68..0b460b0000 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/support/SharedEntityManagerBean.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/support/SharedEntityManagerBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -89,8 +89,7 @@ public class SharedEntityManagerBean extends EntityManagerFactoryAccessor if (emf == null) { throw new IllegalArgumentException("'entityManagerFactory' or 'persistenceUnitName' is required"); } - if (emf instanceof EntityManagerFactoryInfo) { - EntityManagerFactoryInfo emfInfo = (EntityManagerFactoryInfo) emf; + if (emf instanceof EntityManagerFactoryInfo emfInfo) { if (this.entityManagerInterface == null) { this.entityManagerInterface = emfInfo.getEntityManagerInterface(); if (this.entityManagerInterface == null) { diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/vendor/HibernateJpaDialect.java b/spring-orm/src/main/java/org/springframework/orm/jpa/vendor/HibernateJpaDialect.java index a87bf44b65..acf70e0909 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/vendor/HibernateJpaDialect.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/vendor/HibernateJpaDialect.java @@ -241,8 +241,7 @@ public class HibernateJpaDialect extends DefaultJpaDialect { * @return the corresponding DataAccessException instance */ protected DataAccessException convertHibernateAccessException(HibernateException ex) { - if (this.jdbcExceptionTranslator != null && ex instanceof JDBCException) { - JDBCException jdbcEx = (JDBCException) ex; + if (this.jdbcExceptionTranslator != null && ex instanceof JDBCException jdbcEx) { DataAccessException dae = this.jdbcExceptionTranslator.translate( "Hibernate operation: " + jdbcEx.getMessage(), jdbcEx.getSQL(), jdbcEx.getSQLException()); if (dae != null) { @@ -253,29 +252,23 @@ public class HibernateJpaDialect extends DefaultJpaDialect { if (ex instanceof JDBCConnectionException) { return new DataAccessResourceFailureException(ex.getMessage(), ex); } - if (ex instanceof SQLGrammarException) { - SQLGrammarException jdbcEx = (SQLGrammarException) ex; + if (ex instanceof SQLGrammarException jdbcEx) { return new InvalidDataAccessResourceUsageException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } - if (ex instanceof QueryTimeoutException) { - QueryTimeoutException jdbcEx = (QueryTimeoutException) ex; + if (ex instanceof QueryTimeoutException jdbcEx) { return new org.springframework.dao.QueryTimeoutException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } - if (ex instanceof LockAcquisitionException) { - LockAcquisitionException jdbcEx = (LockAcquisitionException) ex; + if (ex instanceof LockAcquisitionException jdbcEx) { return new CannotAcquireLockException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } - if (ex instanceof PessimisticLockException) { - PessimisticLockException jdbcEx = (PessimisticLockException) ex; + if (ex instanceof PessimisticLockException jdbcEx) { return new PessimisticLockingFailureException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } - if (ex instanceof ConstraintViolationException) { - ConstraintViolationException jdbcEx = (ConstraintViolationException) ex; + if (ex instanceof ConstraintViolationException jdbcEx) { return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]; constraint [" + jdbcEx.getConstraintName() + "]", ex); } - if (ex instanceof DataException) { - DataException jdbcEx = (DataException) ex; + if (ex instanceof DataException jdbcEx) { return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } // end of JDBCException subclass handling @@ -301,16 +294,13 @@ public class HibernateJpaDialect extends DefaultJpaDialect { if (ex instanceof ObjectDeletedException) { return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); } - if (ex instanceof UnresolvableObjectException) { - UnresolvableObjectException hibEx = (UnresolvableObjectException) ex; + if (ex instanceof UnresolvableObjectException hibEx) { return new ObjectRetrievalFailureException(hibEx.getEntityName(), hibEx.getIdentifier(), ex.getMessage(), ex); } - if (ex instanceof WrongClassException) { - WrongClassException hibEx = (WrongClassException) ex; + if (ex instanceof WrongClassException hibEx) { return new ObjectRetrievalFailureException(hibEx.getEntityName(), hibEx.getIdentifier(), ex.getMessage(), ex); } - if (ex instanceof StaleObjectStateException) { - StaleObjectStateException hibEx = (StaleObjectStateException) ex; + if (ex instanceof StaleObjectStateException hibEx) { return new ObjectOptimisticLockingFailureException(hibEx.getEntityName(), hibEx.getIdentifier(), ex); } if (ex instanceof StaleStateException) {