gpt4 book ai didi

java - Spring JPA - org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean 无法转换为 javax.persistence.EntityManagerFactory

转载 作者:IT老高 更新时间:2023-10-28 13:55:29 26 4
gpt4 key购买 nike

目前我正在尝试让 JPA 示例与 Spring Boot 一起使用

(http://spring.io/guides/tutorials/data/3/)。

当我使用示例中的代码时:

@Bean
public DataSource dataSource() throws SQLException {

EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
return builder.setType(EmbeddedDatabaseType.H2).build();
}

@Bean
public EntityManagerFactory entityManagerFactory() throws SQLException {

HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);

LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("com.yummynoodlebar.persistence.domain");
factory.setDataSource(dataSource());
factory.afterPropertiesSet();

return factory.getObject();
}

@Bean
public EntityManager entityManager(EntityManagerFactory entityManagerFactory) {
return entityManagerFactory.createEntityManager();
}

@Bean
public PlatformTransactionManager transactionManager() throws SQLException {

JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory());
return txManager;
}

@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator() {
return new HibernateExceptionTranslator();
}

我总是收到异常“原因:java.lang.ClassCastException: org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean$$EnhancerBySpringCGLIB$$3cbaf28d cannot be cast to javax.persistence.EntityManagerFactory”。

我将这个示例与我自己的数据源一起使用:

@Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUsername("user");
dataSource.setMaxTotal(5);
dataSource.setInitialSize(2);
dataSource.setPassword("pw");
dataSource.setUrl("jdbc:mysql://localhost/data");
return dataSource;
}

当我改变一些东西时,一切正常:

我将“EntityManagerFactory”-方法更改为:

public LocalContainerEntityManagerFactoryBean entityManagerFactory() ...

以及transactionManager方法:

public PlatformTransactionManager transactionManager(
EntityManagerFactory emf) throws SQLException {

并通过方法变量“emf”直接设置EntityManagerFactory。

这是为什么呢?

有人可以向我解释为什么教程中的示例不起作用吗?我正在为这个项目使用 mysql 驱动程序。

谢谢!

问候

最佳答案

这可能会有所帮助:

@Bean
public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
transactionManager.setDataSource(dataSource());
transactionManager.setJpaDialect(jpaDialect());
return transactionManager;
}

关于java - Spring JPA - org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean 无法转换为 javax.persistence.EntityManagerFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25185023/

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