You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
DATAJPA-993 - Improved example of infrastructure setup in reference documentation.
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.
Original pull request: #180.
Copy file name to clipboardExpand all lines: src/main/asciidoc/jpa.adoc
+3-4Lines changed: 3 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -65,7 +65,7 @@ class ApplicationConfig {
65
65
}
66
66
67
67
@Bean
68
-
public EntityManagerFactory entityManagerFactory() {
68
+
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
69
69
70
70
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
71
71
vendorAdapter.setGenerateDdl(true);
@@ -74,9 +74,7 @@ class ApplicationConfig {
74
74
factory.setJpaVendorAdapter(vendorAdapter);
75
75
factory.setPackagesToScan("com.acme.domain");
76
76
factory.setDataSource(dataSource());
77
-
factory.afterPropertiesSet();
78
-
79
-
return factory.getObject();
77
+
return factory;
80
78
}
81
79
82
80
@Bean
@@ -89,6 +87,7 @@ class ApplicationConfig {
89
87
}
90
88
----
91
89
====
90
+
NB: It's important to create `LocalContainerEntityManagerFactoryBean` and not `EntityManagerFactory` directly since the former also participates in exception translation mechanisms besides simply creating `EntityManagerFactory`.
92
91
93
92
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.
0 commit comments