本文介绍了如何停用Spring Data异常转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的 org.springframework.orm.hibernate5.HibernateExceptionTranslator 使用了一个延迟,它首先尝试使用vanilla Hibernate映射异常( SessionFactoryUtils中)。如果不能进行翻译,它会通过 EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible()尝试JPA。

后者让我们感到困扰正在将 IllegalStateException 转换为 InvalidDataAccessApiUsageException 。我不确定为什么 IllegalStateException IllegalArgumentException 得到了这种特殊待遇,但是没问题。所以现在我们突然终止了之前没有翻译过的翻译过的例外。由于这些额外的翻译是在JPA的上下文中(因为它被称为 convertJpaAccessExceptionIfPossible ),这也没有意义,因为我们不使用JPA而是使用vanilla Hibernate。



那么,确保我们只获得Hibernate异常转换的正确方法是什么? Afaic HibernateExceptionTranslator 没有关闭JPA转换的选项,并且 LocalSessionFactoryBean 扩展 HibernateExceptionTranslator 没有注入另一个实现的选项。如果JPA没有被实际使用,那么让 HibernateExceptionTranslator JPA不知道并添加另一个翻译器会更有意义吗?

From Spring Data b
$ b

省略@Repository注释,那么vanilla Hibernate异常将不会被翻译。


The new org.springframework.orm.hibernate5.HibernateExceptionTranslator uses a fallthrough in the sense that it first tries to map the exception using vanilla Hibernate (SessionFactoryUtils). If no translation could be done it tries JPA via EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible().

The latter troubles us as it is translating IllegalStateException to InvalidDataAccessApiUsageException. I'm not really sure why IllegalStateException and IllegalArgumentException get this special treatment, but ok. So now we suddenly end up with translated exceptions that weren't translated before. Since these extra translations are in context of JPA (as it is called convertJpaAccessExceptionIfPossible) this also makes no sense as we are not using JPA but vanilla Hibernate.

So, what is the correct way to make sure that we only get Hibernate exceptions translated? Afaic the HibernateExceptionTranslator has no options to turn off JPA translation and the LocalSessionFactoryBean extends HibernateExceptionTranslator without the option to inject another implementation. Wouldn't it make more sense to have the HibernateExceptionTranslator JPA unaware and add another translator in case JPA is actually used?

解决方案

From Spring Data reference:

Omit the @Repository annotation, then vanilla Hibernate exceptions won't get translated.

这篇关于如何停用Spring Data异常转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 12:18