Spring Boot REST + JPA + Hibernate + MySQL - Base Configuration

Find the description of spring boot starter configured in maven file.

Spring boot provides default database configurations when it scans Spring Data JPA in classpath. Spring boot uses spring-boot-starter-data-jpa starter to configure spring JPA with hibernate. For data source we need to configure data source properties starting with spring.datasource.* in application.properties and spring boot JPA will configure data source automatically. Spring boot prefers tomcat pooling on first place then HikariCP and then Commons DBCP on the basis of availability. We need not to write spring configuration classes and just by configuring properties in application.properties we are done. In our DAO class we can get instance of EntityManager using dependency injection. To run the application we will create a class with main() method that will call SpringApplication.run() to start the application with embedded tomcat. The class with main() method will be annotated with @SpringBootApplication


- spring-boot-starter-parent : Parent POM for dependency management. 
- spring-boot-starter-web: Starter for building web, REST applications. It uses tomcat server as default embedded server. 
- spring-boot-starter-data-jpa: Starter for spring data JPA with hibernate. 
- spring-boot-devtools : It provides developer tools. These tools are helpful in application development mode. One of the features of developer tool is automatic restart of the server for any change in code. 
- spring-boot-maven-plugin : It is used to create executable JAR of the application.

Configure Properties in application.properties File

The properties related to database, hibernate and logging needs to be configured in application.properties file. These properties will be automatically read by spring. 

The data source properties starting with spring.datasource.* will automatically be read by spring boot JPA. To change the hibernate properties we will use prefix spring.jpa.properties.* with hibernate property name. On the basis of given data source URL, spring boot can automatically identify data source driver class. So we need not to configure diver class. 
Find the properties to configure JpaBaseConfiguration and HibernateJpaAutoConfiguration in application.properties
- spring.data.jpa.repositories.enabled: It enables JPA repositories. The default value is true
- spring.jpa.database: It targets database to operate on. By default embedded database is auto-detected. 
- spring.jpa.database-platform: It is used to provide the name of database to operate on. By default it is auto- detected. 
- spring.jpa.generate-ddl: It is used to initialize schema on startup. By default the value is false
- spring.jpa.hibernate.ddl-auto: It is DDL mode used for embedded database. Default value is create-drop
- spring.jpa.hibernate.naming.implicit-strategy: It is Hibernate 5 implicit naming strategy fully qualified name. 
- spring.jpa.hibernate.naming.physical-strategy: It is Hibernate 5 physical naming strategy fully qualified name. 
- spring.jpa.hibernate.use-new-id-generator-mappings: It is used for Hibernate IdentifierGenerator for AUTO, TABLE and SEQUENCE. 
- spring.jpa.open-in-view: The default value is true. It binds a JPA EntityManager to the thread for the entire processing of the request. 
- spring.jpa.properties.*: It sets additional native properties to set on the JPA provider. 
- spring.jpa.show-sql: It enables logging of SQL statements. Default value is false.

Sample project

More project source in GitHub

Previous Post Next Post