CrudRepository deleteAllInBatch

CrudRepository#deleteAll and CrudRepository#deleteAll(Iterable entities) methods are used delete the records from the underlying database, but both method issues delete query for each record, so if the requirement is to delete all the records then deleting each record one by one is redundant, to avoid this issue, CrudRepository provides deleteAllInBatch method. deleteAllInBatch method will delete all the records of an entity type in the underlying database in a single query.

schema.sql

drop database if exists AppleDb;
create database AppleDb;
use AppleDb;
CREATE TABLE apple ( apple_id BIGINT NOT NULL, apple_name VARCHAR(255) DEFAULT NULL, PRIMARY KEY (apple_id)) ENGINE=INNODB;
CREATE TABLE hibernate_sequence ( next_val BIGINT) ENGINE=INNODB;

data.sql

insert into hibernate_sequence values ( 1 );
insert into apple (apple_id, apple_name ) values (1, "Macintosh");
insert into apple (apple_id, apple_name ) values (2, "Fuji");
insert into apple (apple_id, apple_name ) values (3, "Gala");
insert into apple (apple_id, apple_name ) values (4, "Jonagold");
insert into apple (apple_id, apple_name ) values (5, "GrannySmith");
insert into apple (apple_id, apple_name ) values (6, "PinkLady");
insert into apple (apple_id, apple_name ) values (7, "Golden Delicious");
insert into apple (apple_id, apple_name ) values (8, "Fireside" );
insert into apple (apple_id, apple_name ) values (9, "Jazz" );
insert into apple (apple_id, apple_name ) values (10, "Mutsu" );
insert into apple (apple_id, apple_name ) values (11, "Pinova" );
package com.sujan.example.jpa.controller;

import com.sujan.example.jpa.entity.Apple;
import com.sujan.example.jpa.repository.AppleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class AppleController {
    @Autowired
    private AppleRepository appleRepository;

    @GetMapping
    void deleteAllInBatch() {
        List<Apple> apples = appleRepository.findAll();
        apples.forEach(System.out::println);
        appleRepository.deleteAllInBatch();
    }
}
package com.sujan.example.jpa.entity;

import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Data
@Entity
@NoArgsConstructor
public class Apple {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long appleId;
    private String appleName;
    private String taste;
}
package com.sujan.example.jpa.repository;

import com.sujan.example.jpa.entity.Apple;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface AppleRepository extends JpaRepository<Apple, Long> {
}
package com.sujan.example.jpa;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class JpaApplication {
    public static void main(String[] args) {
        SpringApplication.run(JpaApplication.class, args);
    }
}
spring.datasource.url=jdbc:mysql://localhost:3306/AppleDb
spring.datasource.username=root
spring.datasource.password=root
plugins {
    id 'org.springframework.boot' version '2.3.3.RELEASE'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}
group = 'com.sujan'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}
repositories {
    mavenCentral()
}
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    annotationProcessor 'org.projectlombok:lombok'
    compileOnly 'org.projectlombok:lombok'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    runtimeOnly 'mysql:mysql-connector-java'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}
test {
    useJUnitPlatform()
}
curl --location --request GET 'http://localhost:8080/'
Hibernate: select apple0_.apple_id as apple_id1_0_, apple0_.apple_name as apple_na2_0_ from apple apple0_
Apple(appleId=1, appleName=Macintosh)
Apple(appleId=2, appleName=Fuji)
Apple(appleId=3, appleName=Gala)
Apple(appleId=4, appleName=Jonagold)
Apple(appleId=5, appleName=GrannySmith)
Apple(appleId=6, appleName=PinkLady)
Apple(appleId=7, appleName=Golden Delicious)
Apple(appleId=8, appleName=Fireside)
Apple(appleId=9, appleName=Jazz)
Apple(appleId=10, appleName=Mutsu)
Apple(appleId=11, appleName=Pinova)
Hibernate: delete from apple

 

follow us on