If a SQLExceptionTranslator bean has been defined, it should be used in the autoconfiguration of JdbcTemplate and LocalContainerEntityManagerFactoryBean.
Otherwise, it is fiddly to guarantee that the translator has been set before the autoconfigured beans are fist used. I think this would do it, but it's going to slow down startup:
@RequiredArgsConstructor
public class SQLExceptionTranslatorBeanPostProcessor implements BeanPostProcessor {
private final SQLExceptionTranslator translator;
public @Nullable Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof AbstractEntityManagerFactoryBean emfb) {
JpaDialect dialect = emfb.getJpaVendorAdapter().getJpaDialect();
if (dialect instanceof HibernateJpaDialect hibernate) {
hibernate.setJdbcExceptionTranslator(translator);
}
} else if (bean instanceof JdbcTemplate template) {
template.setExceptionTranslator(translator);
}
return bean;
}
}
Comment From: snicoll
Closing in favor of PR #43511