Build RESTful APIs with Spring MVC
  • Introduction
  • Overview
  • An introduction to REST
  • Prerequisites
  • Getting Started
    • Project skeleton
    • Configure Spring WebMVC
    • Configure Datasource
    • Configure JPA
    • Configure Spring Security
    • Configure Swagger
    • Maven profiles and Spring profiles
  • Getting started with Spring Boot
    • Project skeleton
    • Configure Datasource
    • Configure JPA
    • Configure Spring Security
    • Configure Swagger
    • Maven profiles and Spring profiles
  • Build REST API
  • Handle Exceptions
  • Test APIs
  • Visualize and document REST APIs
  • Secure APIs
  • Upgrade to Spring Boot 1.4
Powered by GitBook
On this page

Was this helpful?

  1. Getting started with Spring Boot

Configure JPA

Make sure the following dependencies are added in your dependency section of the project pom.xml file.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

It will add managed JPA, Spring Data JPA and Hibernate into your project.

We have already configured DataSource, and JPA and Spring Data JPA via application.yml.

You can also use Java code configuration to add some extra features.

@Configuration
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
@EntityScan(basePackageClasses = {User.class, Jsr310JpaConverters.class})
@EnableJpaAuditing(auditorAwareRef = "auditor")
public class JpaConfig {

    @Bean
    public AuditorAware<User> auditor() {
        return () -> SecurityUtil.currentUser();
    }

}

In the above codes we add @EnableTransactionManagement(mode = AdviceMode.ASPECTJ) to enable transaction management and use AspectJ to weave transaction aspect into your business logic.

And @EnableJpaAuditing(auditorAwareRef = "auditor") enables the simple auditing features provided in Spring Data JPA. It requires a AuditorAware bean.

@EntityScan(basePackageClasses = {User.class, Jsr310JpaConverters.class}) add the JPA entity scan scope, Java 8 DateTime support is added in Spring Data JPA via JPA 2.1 AttributeConvertor feature.

PreviousConfigure DatasourceNextConfigure Spring Security

Last updated 5 years ago

Was this helpful?