diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.java index e87d5fdc65..5bb05bd031 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.java @@ -23,9 +23,11 @@ import javax.sql.DataSource; import org.hibernate.cfg.ImprovedNamingStrategy; import org.hibernate.ejb.HibernateEntityManager; +import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection; import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; @@ -45,10 +47,18 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; HibernateEntityManager.class }) @ConditionalOnBean(DataSource.class) @EnableTransactionManagement -public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration { +public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration implements + BeanClassLoaderAware { private RelaxedPropertyResolver environment; + private ClassLoader classLoader; + + @Override + public void setBeanClassLoader(ClassLoader classLoader) { + this.classLoader = classLoader; + } + @Override public void setEnvironment(Environment environment) { super.setEnvironment(environment); @@ -67,9 +77,18 @@ public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration { Map properties = entityManagerFactoryBean.getJpaPropertyMap(); properties.put("hibernate.ejb.naming_strategy", this.environment.getProperty( "naming-strategy", ImprovedNamingStrategy.class.getName())); - String ddlAuto = this.environment.getProperty("ddl-auto", "none"); + String ddlAuto = this.environment.getProperty("ddl-auto", getDefaultDdlAuto()); if (!"none".equals(ddlAuto)) { properties.put("hibernate.hbm2ddl.auto", ddlAuto); } } + + private String getDefaultDdlAuto() { + EmbeddedDatabaseConnection embeddedDatabaseConnection = EmbeddedDatabaseConnection + .get(this.classLoader); + if (embeddedDatabaseConnection == EmbeddedDatabaseConnection.NONE) { + return "none"; + } + return "create-drop"; + } } diff --git a/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/HotelServiceImpl.java b/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/HotelServiceImpl.java index 14918cc7f6..8c4f6c388e 100644 --- a/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/HotelServiceImpl.java +++ b/spring-boot-samples/spring-boot-sample-data-jpa/src/main/java/org/springframework/boot/sample/data/jpa/service/HotelServiceImpl.java @@ -42,7 +42,7 @@ class HotelServiceImpl implements HotelService { private final HotelRepository hotelRepository; private final ReviewRepository reviewRepository; - + @Autowired public HotelServiceImpl(HotelRepository hotelRepository, ReviewRepository reviewRepository) { diff --git a/spring-boot-samples/spring-boot-sample-data-jpa/src/main/resources/application.properties b/spring-boot-samples/spring-boot-sample-data-jpa/src/main/resources/application.properties deleted file mode 100644 index 948b0917f6..0000000000 --- a/spring-boot-samples/spring-boot-sample-data-jpa/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ -spring.jpa.hibernate.ddl-auto: create-drop