Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot JPA base configuration taking precendence over app configuration

Context: Using Spring Boot 0.5.0-M7, MySQL and Commons DBCP, Java config, @EnableAutoConfiguration set.

I have defined the application's DataSource bean, JpaVendorAdapter bean, LocalContainerEntityManagerFactoryBean and TransactionManager beans with specific configuration to support DBCP and some Hibernate settings. On application startup, the DataSource is definitely being respected (debug confirms), but the other beans are ignored. This appears to be as a result of the JpaBaseConfiguration class, which is brought into the mix by the WebMvcConfiguration and one of it's Security Filters. In any case, among other things the issue is that the Hibernate setting sets the hbm2ddl (Schema export) to drop-create, such that I lose my data every time the server restarts.

I looked through the source and found that there appears to be affordance to set Environment variables that will be respected in the JpaBaseConfiguration, however that seems to defeat setting up beans to do the same job. I have tried excluding some autoconfiguration classes related to Data to no avail. Is there some other expectation for setting up a non-default data configuration I may be missing?

like image 554
hoserdude Avatar asked Jan 21 '14 06:01

hoserdude


People also ask

When configuring an application which configuration is given precedence by spring?

Spring Boot application converts the command line properties into Spring Boot Environment properties. Command line properties take precedence over the other property sources. By default, Spring Boot uses the 8080 port number to start the Tomcat.

Does spring boot Favour configuration over convention?

Spring has always favoured convention over configuration, which means it takes up the majority of working uses cases into consideration and goes by it rather than nit-picking an exact configuration and dependencies required for a specific application development.

How do I override application properties in spring boot?

To override your Spring Boot application properties when it's running on Kubernetes, just set environment variables on the container. To set an environment variable on a container, first, initialise a ConfigMap containing the environment variables that you want to override.

Which configuration in spring data JPA provides database connection details to the application?

Configuring Spring Data JPA We can configure Spring Data JPA by following these steps: Enable Spring Data JPA by annotating the PersistenceContext class with the @EnableJpaRepositories annotation. Configure the base packages that are scanned when Spring Data JPA creates implementations for our repository interfaces.


1 Answers

Looking at the code for Spring Boot 0.5.0.M7 when hibernate is detected (HibernateEntityManager) and Springs LocalContainerEntityManagerFactoryBean hibernate will always be configured by default. You can override certain properties by putting them in the application.properties.

spring.jpa.hibernate.naming-strategy - Will set the naming-strategy used default = ImprovedNamingStrategy. spring.jpa.hibernate.ddl-auto - will set the hibernate.hbm2ddl.auto default = create-drop.

General JPA properties you can set

  1. spring.jpa.show-sql - show sql in the logs
  2. spring.jpa.database-platform - for hibernate this is the dialect
  3. spring.jpa.database - The database used (don't use together with database-platform!).
  4. spring.jpa.generate-ddl - should the ddl be generated, default false (overriden by the spring.jpa.hibernate.ddl-auto property)

If you want to speficy some none default properties prefix them with spring.jpa.properties then they will be added to the jpaProperties of the LocalContainterEntityManagerFactoryBean.

The inclusion of the HibernateJpaAutoConfiguration isn't related to any other configuration it is simply triggered by the detected of the some classes

@ConditionalOnClass({ LocalContainerEntityManagerFactoryBean.class,
    EnableTransactionManagement.class, EntityManager.class,
    HibernateEntityManager.class })
@ConditionalOnBean(DataSource.class)
@EnableTransactionManagement
like image 154
M. Deinum Avatar answered Nov 15 '22 01:11

M. Deinum