Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring JPA - org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean cannot be cast to javax.persistence.EntityManagerFactory

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

( 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".

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:

public LocalContainerEntityManagerFactoryBean entityManagerFactory() ...

and the transactionManager method to:

public PlatformTransactionManager transactionManager(
        EntityManagerFactory emf) throws SQLException {

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

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.

Thank you!

Regards

like image 744
jvecsei Avatar asked Aug 07 '14 14:08

jvecsei


1 Answers

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;
    }
like image 65
Oleksii Kyslytsyn Avatar answered Nov 11 '22 12:11

Oleksii Kyslytsyn