16

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

jvecsei
  • 1,936
  • 2
  • 17
  • 24
  • I'm having exactly the same issue here. – Pedro Dusso Sep 11 '14 at 17:09
  • 1
    This config helps: https://github.com/eugenp/tutorials/blob/master/spring-jpa/src/main/java/org/baeldung/config/PersistenceJPAConfig.java – Pedro Dusso Sep 11 '14 at 20:11
  • i posted that solution^^ i just want to know why the code of the spring.io website doesn't work. – jvecsei Sep 13 '14 at 22:37
  • I have wrote a blog u might be interested into, http://jasenkoh.blogspot.com/2014/11/java-restful-web-services-with-spring.html – jasenkoh Nov 22 '14 at 19:12
  • I have changed the returned type of the entityManagerFactory() method to LocalContainerEntityManagerFactoryBean and it was ok for me. I have a Spring boot project with two different datasources and I had to write transactionManager() entityManagerFactory() etc... for each configuration to differentiate the both datasource usage. – user1842947 Feb 14 '17 at 07:51

2 Answers2

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;
    }
Oleksii Kyslytsyn
  • 2,458
  • 2
  • 27
  • 43
  • Just to spell out Oleksii's answer - move the EntityManagerFactory out to being a method parameter instead of calling the `entityManagerFactory()` method in the transactionManager method itself. (I overlooked this detail initially) – Conor Svensson Oct 08 '15 at 06:08
0

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

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

And import using an annotation:

@ImportResource("classpath*:/config.xml")
Ati
  • 181
  • 2
  • 9