Initially, before implementing the domain model, the dependencies and configuration class need to be specified. The following Maven starter dependency and H2 database dependency need to be included:
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.196</version>
</dependency>
</dependencies>
The following RepoConfig is used, which enables JPA Auditing (here auditing means tracking and logging events related to entities, such as createdBy for the Comment entity); this will enable auditing of the created date and created a user of an entry in the table:
@Configuration
@EnableJpaAuditing
public class RepoConfig {
}
The following configuration properties in the application.properties file need to be set to configure DataSource:
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.show_sql=true
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:~/retroboard
spring.datasource.username=sa
spring.datasource.password=
The preceding configuration does the following:
- Uses spring.jpa.hibernate.ddl-auto to set automatically generate Data Definition Language (DDL) SQL
- Uses spring.jpa.properties.hibernate.format_sql to format SQL generated in a visually pleasing manner
- Uses spring.jpa.properties.hibernate.show_sql to show the SQL generated
- Uses spring.datasource.driver-class-name to set org.h2.Driver as the database driver
- Uses spring.datasource.url to set the JDBC URL
- Uses spring.datasource.username to set the username for the H2 database
- Uses spring.datasource.password to set the password for the H2 database
For the complete set of Spring Boot Database configurations, the following documentation URL can be used:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html