From 4a370eb5380dafe541192b505aac6d0659ee15c5 Mon Sep 17 00:00:00 2001 From: Stanislav Bashkyrtsev Date: Sat, 3 Sep 2016 20:00:33 +0300 Subject: [PATCH] Fix docs on creation of EntityManagerFactory Previously EntityManagerFactory was created manually which resulted in `LocalContainerEntityManagerFactoryBean` not being exposed to ApplicationContext. The latter is an important bean since it enables exception translation. The fact that factory bean implements additional mechanisms is probably a mistake in the design. But I guess we have to live with this now. --- src/main/asciidoc/jpa.adoc | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/asciidoc/jpa.adoc b/src/main/asciidoc/jpa.adoc index d4e9d5816a..a98686a62c 100644 --- a/src/main/asciidoc/jpa.adoc +++ b/src/main/asciidoc/jpa.adoc @@ -65,7 +65,7 @@ class ApplicationConfig { } @Bean - public EntityManagerFactory entityManagerFactory() { + public LocalContainerEntityManagerFactoryBean entityManagerFactory() { HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); vendorAdapter.setGenerateDdl(true); @@ -74,9 +74,7 @@ class ApplicationConfig { factory.setJpaVendorAdapter(vendorAdapter); factory.setPackagesToScan("com.acme.domain"); factory.setDataSource(dataSource()); - factory.afterPropertiesSet(); - - return factory.getObject(); + return factory; } @Bean @@ -89,6 +87,7 @@ class ApplicationConfig { } ---- ==== +NB: It's important to create `LocalContainerEntityManagerFactoryBean` and not `EntityManagerFactory` directly since the former also participates in exception translation mechanisms besides simply creating `EntityManagerFactory`. The just shown configuration class sets up an embedded HSQL database using the `EmbeddedDatabaseBuilder` API of spring-jdbc. We then set up a `EntityManagerFactory` and use Hibernate as sample persistence provider. The last infrastructure component declared here is the `JpaTransactionManager`. We finally activate Spring Data JPA repositories using the `@EnableJpaRepositories` annotation which essentially carries the same attributes as the XML namespace does. If no base package is configured it will use the one the configuration class resides in.