diff --git a/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/unidirectionalcascadingdelete/Article.java b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/unidirectionalcascadingdelete/Article.java new file mode 100644 index 0000000000..97f79515cc --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/unidirectionalcascadingdelete/Article.java @@ -0,0 +1,46 @@ +package com.baeldung.spring.data.persistence.unidirectionalcascadingdelete; + +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToMany; + +@Entity +public class Article { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + private String name; + @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) + private Set comments = new HashSet<>(); + + public Set getComments() { + return comments; + } + + public long getId() { + return id; + } + + public String getName() { + return name; + } + + public void setId(long id) { + this.id = id; + } + + public void setName(String name) { + this.name = name; + } + + public void setComments(Set comments) { + this.comments = comments; + } +} diff --git a/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/unidirectionalcascadingdelete/ArticleRepository.java b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/unidirectionalcascadingdelete/ArticleRepository.java new file mode 100644 index 0000000000..7cf88c73b1 --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/unidirectionalcascadingdelete/ArticleRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.spring.data.persistence.unidirectionalcascadingdelete; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ArticleRepository extends JpaRepository { + +} diff --git a/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/unidirectionalcascadingdelete/ArticleService.java b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/unidirectionalcascadingdelete/ArticleService.java new file mode 100644 index 0000000000..b0f9bdb05e --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/unidirectionalcascadingdelete/ArticleService.java @@ -0,0 +1,17 @@ +package com.baeldung.spring.data.persistence.unidirectionalcascadingdelete; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class ArticleService { + + @Autowired + private ArticleRepository articleRepository; + + public void deleteArticle(Article article) { + articleRepository.delete(article); + + } + +} diff --git a/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/unidirectionalcascadingdelete/CascadingDeleteApplication.java b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/unidirectionalcascadingdelete/CascadingDeleteApplication.java new file mode 100644 index 0000000000..1c417d9cc9 --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/unidirectionalcascadingdelete/CascadingDeleteApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.spring.data.persistence.unidirectionalcascadingdelete; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class CascadingDeleteApplication { + public static void main(String[] args) { + SpringApplication.run(CascadingDeleteApplication.class, args); + } + +} diff --git a/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/unidirectionalcascadingdelete/Comment.java b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/unidirectionalcascadingdelete/Comment.java new file mode 100644 index 0000000000..334ac01ea5 --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/unidirectionalcascadingdelete/Comment.java @@ -0,0 +1,45 @@ +package com.baeldung.spring.data.persistence.unidirectionalcascadingdelete; + +import java.util.Date; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Comment { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + + private String description; + private Date date; + + public void setId(long id) { + this.id = id; + } + + public void setDescription(String description) { + this.description = description; + } + + public void setDate(Date total) { + this.date = total; + } + + public long getId() { + return id; + } + + public String getDescription() { + return description; + } + + public Date getDate() { + return date; + } + +} + diff --git a/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/unidirectionalcascadingdelete/CommentRepository.java b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/unidirectionalcascadingdelete/CommentRepository.java new file mode 100644 index 0000000000..cadb1d7173 --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/unidirectionalcascadingdelete/CommentRepository.java @@ -0,0 +1,8 @@ +package com.baeldung.spring.data.persistence.unidirectionalcascadingdelete; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface CommentRepository extends JpaRepository { +} diff --git a/persistence-modules/spring-data-jpa-repo-2/src/test/java/com/baeldung/spring/data/persistence/deletionCascading/ArticleRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa-repo-2/src/test/java/com/baeldung/spring/data/persistence/deletionCascading/ArticleRepositoryIntegrationTest.java new file mode 100644 index 0000000000..6ff8d57b6e --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-2/src/test/java/com/baeldung/spring/data/persistence/deletionCascading/ArticleRepositoryIntegrationTest.java @@ -0,0 +1,75 @@ +package com.baeldung.spring.data.persistence.deletionCascading; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.Date; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import javax.transaction.Transactional; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import com.baeldung.spring.data.persistence.unidirectionalcascadingdelete.Article; +import com.baeldung.spring.data.persistence.unidirectionalcascadingdelete.ArticleRepository; +import com.baeldung.spring.data.persistence.unidirectionalcascadingdelete.ArticleService; +import com.baeldung.spring.data.persistence.unidirectionalcascadingdelete.CascadingDeleteApplication; +import com.baeldung.spring.data.persistence.unidirectionalcascadingdelete.Comment; +import com.baeldung.spring.data.persistence.unidirectionalcascadingdelete.CommentRepository; + +@SpringBootTest(classes = CascadingDeleteApplication.class) +@Transactional +public class ArticleRepositoryIntegrationTest { + + @Autowired + private ArticleRepository articleRepository; + + @Autowired + private CommentRepository commentRepository; + + @Autowired + private ArticleService articleService; + + @Test + @Transactional + public void givenAnArticleAndItsComments_whenDeleteArticle_thenCommentsDeletedAutomatically() { + + Set comments = new HashSet<>(); + Article article = new Article(); + article.setName("introduction to Spring"); + + Comment comment1 = new Comment(); + comment1.setDescription("Explain types of Autowired"); + comment1.setDate(Date.from(Instant.now())); + + Comment comment2 = new Comment(); + comment2.setDescription("Good article"); + comment2.setDate(Date.from(Instant.now() + .minus(10, ChronoUnit.MINUTES))); + + comments.add(comment1); + comments.add(comment2); + + article.setComments(comments); + articleRepository.save(article); + + List
articles = articleRepository.findAll(); + assertThat(articles.size()).isEqualTo(1); + Article retrivedArticle = articles.get(0); + + List fetchedComments = commentRepository.findAll(); + assertThat(fetchedComments.size()).isEqualTo(2); + + articleService.deleteArticle(retrivedArticle); + assertThat(articleRepository.findAll()).isEmpty(); + + assertThat(commentRepository.findAll()).isEmpty(); + + } +} +