Spring Data JPA CrudRepository - saveAll() Method

In this tutorial, we will learn how to use the Spring Data CrudRepository interface provided the saveAll() method with an example. 

YouTube Video

saveAll() Method Overview

As the name depicts, the saveAll() method allows us to save multiple entities to the DB. 

It belongs to the CrudRepository interface defined by Spring Data.

The saveAll() method returns a list of Iterable objects.

Maven Dependencies

Add the following maven dependencies to your Spring Boot project:


Product Entity

Let's first create a Product entity that we are going to save into the database using the save() method:

package net.javaguides.springdatajpacourse.entity;

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import jakarta.persistence.*;
import java.math.BigDecimal;
import java.util.Date;

public class Product {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "sku")
    private String sku;

    @Column(name = "name")
    private String name;

    @Column(name = "description")
    private String description;

    @Column(name = "price")
    private BigDecimal price;

    @Column(name = "image_url")
    private String imageUrl;

    @Column(name = "active")
    private boolean active;

    @Column(name = "date_created")
    private Date dateCreated;

    @Column(name = "last_updated")
    private Date lastUpdated;

    public Long getId() {
        return id;

    public void setId(Long id) {
        this.id = id;

    public String getSku() {
        return sku;

    public void setSku(String sku) {
        this.sku = sku;

    public String getName() {
        return name;

    public void setName(String name) {
        this.name = name;

    public String getDescription() {
        return description;

    public void setDescription(String description) {
        this.description = description;

    public BigDecimal getPrice() {
        return price;

    public void setPrice(BigDecimal price) {
        this.price = price;

    public String getImageUrl() {
        return imageUrl;

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;

    public boolean isActive() {
        return active;

    public void setActive(boolean active) {
        this.active = active;

    public Date getDateCreated() {
        return dateCreated;

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;

    public Date getLastUpdated() {
        return lastUpdated;

    public void setLastUpdated(Date lastUpdated) {
        this.lastUpdated = lastUpdated;

    public String toString() {
        return "Product{" +
                "id=" + id +
                ", sku='" + sku + '\'' +
                ", name='" + name + '\'' +
                ", description='" + description + '\'' +
                ", price=" + price +
                ", imageUrl='" + imageUrl + '\'' +
                ", active=" + active +
                ", dateCreated=" + dateCreated +
                ", lastUpdated=" + lastUpdated +


Let's create ProductRepository which extends the CrudRepository interface. As we know that CrudRepository interface provides the saveAll() method so our ProductRepository interface should extend to the CrudRepository interface to get all its methods:

import net.javaguides.springdatajpacourse.entity.Product;
import org.springframework.data.repository.CrudRepository;

public interface ProductRepository extends CrudRepository<Product, Long> {


Configure MySQL and Hibernate Properties

Let's use the MySQL database to store and retrieve the data in this example and we gonna use Hibernate properties to create and drop tables.

Open the application.properties file and add the following configuration to it:


spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect

spring.jpa.hibernate.ddl-auto = create-drop

Make sure that you will create ecommerce database before running the Spring boot application.
Also, change the MySQL username and password as per your MySQL installation on your machine.

Testing saveAll() Method

In order to test the saveAll() method, we gonna use CommandLineRunner.run() method to execute the testing code while the Spring boot application startup: 
import net.javaguides.springdatajpacourse.entity.Product;
import net.javaguides.springdatajpacourse.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public class SpringDataJpaCourseApplication implements CommandLineRunner{

	public static void main(String[] args) throws Exception {
		SpringApplication.run(SpringDataJpaCourseApplication.class, args);

	private ProductRepository productRepository;

	public void run(String... args) throws Exception {
		Product product = new Product();
        product.setName("product 1");
        product.setDescription("product 1 desc");
        product.setPrice(new BigDecimal(100));
        product.setDateCreated(new Date());
        product.setLastUpdated(new Date());
        product.setSku("product 1 sku");

        Product product2 = new Product();
        product2.setName("product 2");
        product2.setDescription("product 2 desc");
        product2.setPrice(new BigDecimal(200));
        product2.setDateCreated(new Date());
        product2.setLastUpdated(new Date());
        product2.setSku("product 2 sku");

                // save multiple products at once
        productRepository.saveAll(List.of(product, product2));
After finished Spring boot application runs, you can able to see Hibernate generate two INSERT SQL statements in a console:
        (active, date_created, description, image_url, last_updated, name, price, sku) 
        (?, ?, ?, ?, ?, ?, ?, ?)
        (active, date_created, description, image_url, last_updated, name, price, sku) 
        (?, ?, ?, ?, ?, ?, ?, ?)
Note that there are two INSERT SQL statements.

Related Spring Data CrudRepository Tutorials