这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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 +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}

}