Use Hibernate native APIs to defer processing DDL

The EntityManagerFactory will happily process the DDL on startup, but
that happens too early (because of LoadtimeWeaverAware processing). We
can defer it to a more civilised stage, e.g. ContextRefreshedEvent by
using the Hibernate native APIs directly.

It makes the JpaProperties slightly more complex because they need
to distinguish between the early init and late processing versions
of the Hibernate properties.

Not ready for prime time yet because there is no way to deal with
multiple EntityManagers.

Fixes gh-894
This commit is contained in:
Dave Syer
2014-05-18 09:51:35 +01:00
parent 2cc5bdfa09
commit f2e3d94fa1
6 changed files with 98 additions and 47 deletions

View File

@@ -16,6 +16,8 @@
package org.springframework.boot.autoconfigure.orm.jpa;
import javax.sql.DataSource;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
@@ -26,7 +28,6 @@ import org.springframework.boot.autoconfigure.orm.jpa.test.City;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
@@ -58,11 +59,12 @@ public class CustomHibernateJpaAutoConfigurationTests {
PropertyPlaceholderAutoConfiguration.class,
HibernateJpaAutoConfiguration.class);
this.context.refresh();
LocalContainerEntityManagerFactoryBean bean = this.context
.getBean(LocalContainerEntityManagerFactoryBean.class);
String actual = (String) bean.getJpaPropertyMap().get("hibernate.hbm2ddl.auto");
// No default (let Hibernate choose)
assertThat(actual, equalTo(null));
JpaProperties bean = this.context.getBean(JpaProperties.class);
DataSource dataSource = this.context.getBean(DataSource.class);
String actual = (String) bean.getHibernateProperties(dataSource).get(
"hibernate.hbm2ddl.auto");
// Default is generic and safe
assertThat(actual, equalTo("none"));
}
@Test
@@ -74,9 +76,10 @@ public class CustomHibernateJpaAutoConfigurationTests {
PropertyPlaceholderAutoConfiguration.class,
HibernateJpaAutoConfiguration.class);
this.context.refresh();
LocalContainerEntityManagerFactoryBean bean = this.context
.getBean(LocalContainerEntityManagerFactoryBean.class);
String actual = (String) bean.getJpaPropertyMap().get("hibernate.hbm2ddl.auto");
JpaProperties bean = this.context.getBean(JpaProperties.class);
DataSource dataSource = this.context.getBean(DataSource.class);
String actual = (String) bean.getHibernateProperties(dataSource).get(
"hibernate.hbm2ddl.auto");
assertThat(actual, equalTo("create-drop"));
}