Spring JPA - org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean 不能 cast to javax.persistence.EntityManagerFactory

2022-09-03 15:54:20

目前,我试图让JPA示例与弹簧靴一起使用

( 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();
}

我总是得到例外“Caused by: java.lang.ClassCastException: org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean$$EnhancerBySpringCGLIB$$3cbaf28d 不能被投射到 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() ...

和事务管理器方法,以:

public PlatformTransactionManager transactionManager(
        EntityManagerFactory emf) throws SQLException {

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

为什么?

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

谢谢!

问候


答案 1

这可能会有所帮助:

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

答案 2

尝试在 XML 配置中创建事务管理器,如下所示:

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

并使用注释导入:

@ImportResource("classpath*:/config.xml")

推荐