Spring Boot Starter Data JPA: How It Works Internally

Rakesh singhania
3 min readOct 9, 2023

First thing first

JPA (Java persistence API) is not a tool or not a framework its just

Enables seamless interaction between Java Objects and Relational Databases for data access, persistence, and management.

Spring Boot Starter Data JPA is a convenience dependency that provides all of the necessary dependencies to use Spring Data JPA in a Spring Boot application.

It internally uses the Spring Boot JPA dependency, which provides the following:

  • A DataSource bean that is automatically configured based on the database connection settings in the application.properties file or you can set these properties manually also.
@Configuration
public class DataSourceConfig {

@Bean
public DataSource getDataSource() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("com.mysql.cj.jdbc.Driver");
dataSourceBuilder.url("jdbc:mysql://localhost:3306/your_db");
dataSourceBuilder.username("test");
dataSourceBuilder.password("test");
return dataSourceBuilder.build();
}
}
  • A JPA EntityManagerFactory bean that is automatically configured to use the specified DataSource and JPA implementation.
  • A JPA TransactionManager bean that is automatically configured to use the specified EntityManagerFactory.

In addition to these beans, Spring Boot JPA also automatically configures the following:

  • A LocalContainerEntityManagerFactoryBean bean that is responsible for creating and managing the EntityManagerFactory.
  • A JpaVendorAdapter bean that implements the JPA Vendor API and provides information about the specific JPA implementation that is being used.
  • A HibernateProperties bean that contains the Hibernate configuration properties.

How Spring Boot Starter Data JPA works internally

When Spring Boot starts up, it scans the classpath for Spring Boot Starter dependencies. When it finds the spring-boot-starter-data-jpa dependency, it automatically configures all of the necessary beans to use Spring Data JPA.

This includes creating

  • DataSource bean.
  • EntityManagerFactory bean.
  • TransactionManager bean.

Spring Boot also automatically configures the JpaVendorAdapter bean , LocalContainerEntityManagerFactoryBean bean and the HibernateProperties bean.

Once all of these beans have been configured, Spring Boot is ready to use Spring Data JPA. Developers can then

Use Spring Data JPA to interact with their database

in a variety of ways, such as creating and deleting entities, performing queries, and managing transactions.

Example:

The following code shows an example of how to use Spring Boot Starter Data JPA to create a simple entity and persist it to a database:


import javax.persistence.*;

@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)

private Long id;
private String name;
public Customer() {
}
public Customer(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Service

public class CustomerService {

@Autowired
private EntityManager entityManager;

public void createCustomer(User user) {
entityManager.persist(user);
}
public Customer findCustomerById(Long id) {
return entityManager.find(Customer.class, id);
}
}
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

Once the application is running, you can use the UserService class to create and retrieve customers from the database. For example, the following code shows how to create a new customer and persist it to the database:

CustomerService customerService = new CustomerService();
Customer customer = new Customer("Rakesh singhania");
customerService.createCustomer(customer);

You can then retrieve the user from the database by using the following code:

Customer customer = customerService.findCustomerById(id);

Spring Boot Starter Data JPA makes it very easy to use Spring Data JPA in a Spring Boot application.

As you can observe from above explanation that It automatically configures all of the necessary beans, so developers can focus on writing business logic instead of having to worry about plumbing code.

Hope this helps ..

--

--

Rakesh singhania

As a student of technology, each day I take a single step forward on the path of learning.