[BAEL-6041] add code for cascading delete (#13699)
Co-authored-by: uzma <u.khan@proxymity.io>
This commit is contained in:
parent
be7213b147
commit
ae6493b568
|
@ -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<Comment> comments = new HashSet<>();
|
||||||
|
|
||||||
|
public Set<Comment> 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<Comment> comments) {
|
||||||
|
this.comments = comments;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<Article, Long> {
|
||||||
|
|
||||||
|
}
|
|
@ -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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -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<Comment, Long> {
|
||||||
|
}
|
|
@ -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<Comment> 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<Article> articles = articleRepository.findAll();
|
||||||
|
assertThat(articles.size()).isEqualTo(1);
|
||||||
|
Article retrivedArticle = articles.get(0);
|
||||||
|
|
||||||
|
List<Comment> fetchedComments = commentRepository.findAll();
|
||||||
|
assertThat(fetchedComments.size()).isEqualTo(2);
|
||||||
|
|
||||||
|
articleService.deleteArticle(retrivedArticle);
|
||||||
|
assertThat(articleRepository.findAll()).isEmpty();
|
||||||
|
|
||||||
|
assertThat(commentRepository.findAll()).isEmpty();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue