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

时间:2022-09-11 16:44:01

at the moment im trying to get the JPA example working with spring boot

目前我试图让JPA示例使用spring boot

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

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

When I use the code from the example:

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

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

I always get the exception "Caused by: java.lang.ClassCastException: org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean$$EnhancerBySpringCGLIB$$3cbaf28d cannot be cast to javax.persistence.EntityManagerFactory".

我总是得到异常“引起:java.lang.ClassCastException:org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean $$ EnhancerBySpringCGLIB $$ 3cbaf28d无法转换为javax.persistence.EntityManagerFactory”。

I'm using this example with my own datasource:

我在自己的数据源中使用此示例:

@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;
    }

When I am chaning some things everything works fine:

当我正在处理一些事情时,一切正常:

I change the "EntityManagerFactory"-Method to:

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

public LocalContainerEntityManagerFactoryBean entityManagerFactory() ...

and the transactionManager method to:

和transactionManager方法:

public PlatformTransactionManager transactionManager(
        EntityManagerFactory emf) throws SQLException {

and set the EntityManagerFactory directly via the method variable "emf".

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

Why is that?

这是为什么?

Can someone explain to me why the example from the tutorial doesn't work? I'm using the mysql driver for this project.

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

Thank you!

谢谢!

Regards

问候

2 个解决方案

#1


6  

this might help:

这可能有所帮助:

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

#2


0  

Try to create the TransactionManager in an XML configuration, like this:

尝试在XML配置中创建TransactionManager,如下所示:

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

And import using an annotation:

并使用注释导入:

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

#1


6  

this might help:

这可能有所帮助:

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

#2


0  

Try to create the TransactionManager in an XML configuration, like this:

尝试在XML配置中创建TransactionManager,如下所示:

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

And import using an annotation:

并使用注释导入:

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