gpt4 book ai didi

java - 定义 HibernateExceptionTranslator : No persistence exception translators found in bean factory

转载 作者:行者123 更新时间:2023-11-30 06:32:05 40 4
gpt4 key购买 nike

我正在尝试在我的项目中注入(inject)一个 eventRepository,它是 Spring Data Repository:

public class App {

protected static EntityManagerFactory factory;

@Autowired
protected EventRepository eventRepository;

public void execute() {
Event foo = eventRepository.findBySlug("abraxas");
}

public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans.xml");

App runner = (App) context.getBean("AppBean");
runner.execute();
}
}

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<bean id="AppBean" class="org.app.App"></bean>
<jpa:repositories base-package="org.app.repository" />
</beans>

但是当我运行它时出现以下异常:

java.lang.IllegalStateException: No persistence exception translators found in bean factory. Cannot perform exception translation.at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.detectPersistenceExceptionTranslators

在一些评论中,我发现我需要“配置 HibernateExceptionTranslator”,但我没有找到如何配置。

我正在尝试关注 official documentation其中没有提到 HibernateExceptionTranslator 的配置。

谢谢

最佳答案

显然解决方案是添加

     <bean class="org.springframework.orm.hibernate4.HibernateExceptionTranslator"/>

到 beans.xml

感谢 JB Nizet 找到解决方案.

关于java - 定义 HibernateExceptionTranslator : No persistence exception translators found in bean factory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9136202/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com