BAEL-6004 TRUNCATE table in Spring Data JPA (#13554)

This commit is contained in:
Mateusz Szablak 2023-03-08 15:43:16 +01:00 committed by GitHub
parent 75b6c41819
commit c119a05849
6 changed files with 192 additions and 0 deletions

View File

@ -0,0 +1,22 @@
package com.baeldung.spring.data.persistence.truncate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@Repository
public class EntityManagerRepository {
@PersistenceContext
private EntityManager entityManager;
@Transactional
public void truncateTable(String tableName) {
String sql = "TRUNCATE TABLE " + tableName;
Query query = entityManager.createNativeQuery(sql);
query.executeUpdate();
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.spring.data.persistence.truncate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
public class JdbcTemplateRepository {
private final JdbcTemplate jdbcTemplate;
public JdbcTemplateRepository(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Transactional
public void truncateTable(String tableName) {
String sql = "TRUNCATE TABLE " + tableName;
jdbcTemplate.execute(sql);
}
}

View File

@ -0,0 +1,45 @@
package com.baeldung.spring.data.persistence.truncate;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Objects;
@Entity
@Table(name = MyEntity.TABLE_NAME)
public class MyEntity {
public final static String TABLE_NAME = "my_entity";
@Id
@GeneratedValue
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyEntity myEntity = (MyEntity) o;
return Objects.equals(id, myEntity.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "MyEntity{" +
"id=" + id +
'}';
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.spring.data.persistence.truncate;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import javax.transaction.Transactional;
@Repository
public interface MyEntityRepository extends CrudRepository<MyEntity, Long> {
@Modifying
@Transactional
// need to wrap in double quotes due to
// spring.jpa.properties.hibernate.globally_quoted_identifiers=true backward compatibility
// property disabled in tests
@Query(value = "truncate table " + MyEntity.TABLE_NAME, nativeQuery = true)
void truncateTable();
}

View File

@ -0,0 +1,14 @@
package com.baeldung.spring.data.persistence.truncate;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
@EntityScan(basePackageClasses = MyEntity.class)
@SpringBootApplication
public class TruncateSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(TruncateSpringBootApplication.class);
}
}

View File

@ -0,0 +1,71 @@
package com.baeldung.spring.data.persistence.truncate;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(classes = TruncateSpringBootApplication.class,
properties = "spring.jpa.properties.hibernate.globally_quoted_identifiers=false")
class TruncateIntegrationTest {
@Autowired
private MyEntityRepository myEntityRepository;
@Autowired
private JdbcTemplateRepository jdbcTemplateRepository;
@Autowired
private EntityManagerRepository entityManagerRepository;
@Test
void givenSomeData_whenRepositoryTruncateDate_thenNoDataLeft() {
int givenCount = 3;
givenDummyData(givenCount);
myEntityRepository.truncateTable();
assertThat(myEntityRepository.count())
.isNotEqualTo(givenCount)
.isZero();
}
@Test
void givenSomeData_whenEntityManagerTruncateDate_thenNoDataLeft() {
int givenCount = 3;
givenDummyData(givenCount);
entityManagerRepository.truncateTable(MyEntity.TABLE_NAME);
assertThat(myEntityRepository.count())
.isNotEqualTo(givenCount)
.isZero();
}
@Test
void givenSomeData_whenJDBCTemplateTruncateDate_thenNoDataLeft() {
int givenCount = 3;
givenDummyData(givenCount);
jdbcTemplateRepository.truncateTable(MyEntity.TABLE_NAME);
assertThat(myEntityRepository.count())
.isNotEqualTo(givenCount)
.isZero();
}
private void givenDummyData(int count) {
List<MyEntity> input = Stream.generate(this::givenMyEntity).limit(count).collect(Collectors.toList());
myEntityRepository.saveAll(input);
}
private MyEntity givenMyEntity() {
return new MyEntity();
}
}