Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the Java configuration version of jpa:repositories tag?

I'm trying to configure JPA using just Java.

I got the idea that @EnableJpaRepositories would be the equivalent of jpa:repositories tag in xml, but I guess this is not the case?

I have this in my xml:

<jpa:repositories base-package="com.myapp.bla.bla" />

But if I remove it and instead use

@EnableJpaRepositories("com.myapp.bla.bla")

In my java config, I get an exception - I thought it was possible to configure JPA with Java since 1.2.0?

EDIT:

The root exception is:

No bean named 'entityManagerFactory' is defined

I assume the exception has to do with this definition in my config, but as said, everything works if I keep the xml and import it to my java config.

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() throws ClassNotFoundException {
    LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();

    factoryBean.setDataSource(dataSource());
    factoryBean.setPackagesToScan(new String[] { "com.myapp.bla.bla.model" });
    factoryBean.setPersistenceProviderClass(HibernatePersistence.class);
    Properties props = new Properties();
    props.put("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
    factoryBean.setJpaProperties(props);

    return factoryBean;
}
like image 311
wannabeartist Avatar asked Feb 11 '13 18:02

wannabeartist


1 Answers

The problem is that your current configuration creates a bean called entityManagerFactoryBean. However, the error message of your root exception says that a bean named entityManagerFactory is not found.

You have two options for fixing this problem (pick the one you like the most):

  1. Change the name of the method which configures the LocalContainerEntityManagerFactoryBean from entityManagerFactoryBean() to entityManagerFactory(). This creates a bean named entityManagerFactory.
  2. Set the name attribute of the @Bean annotation to "entityManagerFactory". In other words, annotate the configuration method with @Bean(name="entityManagerFactory") annotation. This way you can specify the name of bean yourself and ensure that the name of the annotated method is ignored.
like image 105
pkainulainen Avatar answered Sep 27 '22 16:09

pkainulainen