Skip to content

Commit 6796ec3

Browse files
dionisPriftimaibin
authored andcommitted
BAEL-2810: Finished the examples and unit tests. (eugenp#6732)
1 parent 2cc1de2 commit 6796ec3

File tree

6 files changed

+271
-0
lines changed

6 files changed

+271
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.datajpadelete.entity;
2+
3+
import javax.persistence.*;
4+
5+
@Entity
6+
public class Book {
7+
8+
@Id
9+
@GeneratedValue
10+
private Long id;
11+
private String title;
12+
13+
@ManyToOne
14+
private Category category;
15+
16+
public Book() {
17+
}
18+
19+
public Book(String title) {
20+
this.title = title;
21+
}
22+
23+
public Book(String title, Category category) {
24+
this.title = title;
25+
this.category = category;
26+
}
27+
28+
public Long getId() {
29+
return id;
30+
}
31+
32+
public void setId(Long id) {
33+
this.id = id;
34+
}
35+
36+
public String getTitle() {
37+
return title;
38+
}
39+
40+
public void setTitle(String title) {
41+
this.title = title;
42+
}
43+
44+
public Category getCategory() {
45+
return category;
46+
}
47+
48+
public void setCategory(Category category) {
49+
this.category = category;
50+
}
51+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.datajpadelete.entity;
2+
3+
import javax.persistence.*;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
import java.util.stream.Stream;
7+
8+
@Entity
9+
public class Category {
10+
11+
@Id
12+
@GeneratedValue
13+
private Long id;
14+
private String name;
15+
16+
@OneToMany(mappedBy = "category", cascade = CascadeType.ALL, orphanRemoval = true)
17+
private List<Book> books;
18+
19+
public Category() {
20+
}
21+
22+
public Category(String name) {
23+
this.name = name;
24+
}
25+
26+
public Category(String name, Book... books) {
27+
this.name = name;
28+
this.books = Stream.of(books).collect(Collectors.toList());
29+
this.books.forEach(x -> x.setCategory(this));
30+
}
31+
32+
public Category(String name, List<Book> books) {
33+
this.name = name;
34+
this.books = books;
35+
}
36+
37+
public Long getId() {
38+
return id;
39+
}
40+
41+
public void setId(Long id) {
42+
this.id = id;
43+
}
44+
45+
public String getName() {
46+
return name;
47+
}
48+
49+
public void setName(String name) {
50+
this.name = name;
51+
}
52+
53+
public List<Book> getBooks() {
54+
return books;
55+
}
56+
57+
public void setBooks(List<Book> books) {
58+
this.books = books;
59+
}
60+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.datajpadelete.repository;
2+
3+
import com.baeldung.datajpadelete.entity.Book;
4+
import org.springframework.data.jpa.repository.Modifying;
5+
import org.springframework.data.jpa.repository.Query;
6+
import org.springframework.data.repository.CrudRepository;
7+
import org.springframework.data.repository.query.Param;
8+
import org.springframework.stereotype.Repository;
9+
10+
@Repository
11+
public interface BookRepository extends CrudRepository<Book, Long> {
12+
13+
long deleteByTitle(String title);
14+
15+
@Modifying
16+
@Query("delete from Book b where b.title=:title")
17+
void deleteBooks(@Param("title") String title);
18+
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.datajpadelete.repository;
2+
3+
import com.baeldung.datajpadelete.entity.Category;
4+
import org.springframework.data.repository.CrudRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface CategoryRepository extends CrudRepository<Category, Long> {
9+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.baeldung.datajpadelete;
2+
3+
import com.baeldung.Application;
4+
import com.baeldung.datajpadelete.entity.Book;
5+
import com.baeldung.datajpadelete.repository.BookRepository;
6+
import org.junit.After;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.test.context.junit4.SpringRunner;
13+
import org.springframework.transaction.annotation.Transactional;
14+
15+
import java.util.Arrays;
16+
17+
import static org.assertj.core.api.Assertions.assertThat;
18+
19+
@RunWith(SpringRunner.class)
20+
@SpringBootTest(classes = {Application.class})
21+
public class DeleteFromRepositoryUnitTest {
22+
23+
@Autowired
24+
private BookRepository repository;
25+
26+
Book book1;
27+
Book book2;
28+
29+
@Before
30+
public void setup() {
31+
book1 = new Book("The Hobbit");
32+
book2 = new Book("All Quiet on the Western Front");
33+
34+
repository.saveAll(Arrays.asList(book1, book2));
35+
}
36+
37+
@After
38+
public void teardown() {
39+
repository.deleteAll();
40+
}
41+
42+
@Test
43+
public void whenDeleteByIdFromRepository_thenDeletingShouldBeSuccessful() {
44+
repository.deleteById(book1.getId());
45+
46+
assertThat(repository.count() == 1).isTrue();
47+
}
48+
49+
@Test
50+
public void whenDeleteAllFromRepository_thenRepositoryShouldBeEmpty() {
51+
repository.deleteAll();
52+
53+
assertThat(repository.count() == 0).isTrue();
54+
}
55+
56+
@Test
57+
@Transactional
58+
public void whenDeleteFromDerivedQuery_thenDeletingShouldBeSuccessful() {
59+
repository.deleteByTitle("The Hobbit");
60+
61+
assertThat(repository.count() == 1).isTrue();
62+
}
63+
64+
@Test
65+
@Transactional
66+
public void whenDeleteFromCustomQuery_thenDeletingShouldBeSuccessful() {
67+
repository.deleteBooks("The Hobbit");
68+
69+
assertThat(repository.count() == 1).isTrue();
70+
}
71+
72+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.datajpadelete;
2+
3+
import com.baeldung.Application;
4+
import com.baeldung.datajpadelete.entity.Book;
5+
import com.baeldung.datajpadelete.entity.Category;
6+
import com.baeldung.datajpadelete.repository.BookRepository;
7+
import com.baeldung.datajpadelete.repository.CategoryRepository;
8+
import org.junit.After;
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.boot.test.context.SpringBootTest;
14+
import org.springframework.test.context.junit4.SpringRunner;
15+
16+
import static org.assertj.core.api.Assertions.assertThat;
17+
18+
@RunWith(SpringRunner.class)
19+
@SpringBootTest(classes = {Application.class})
20+
public class DeleteInRelationshipsUnitTest {
21+
22+
@Autowired
23+
private BookRepository bookRepository;
24+
25+
@Autowired
26+
private CategoryRepository categoryRepository;
27+
28+
@Before
29+
public void setup() {
30+
Book book1 = new Book("The Hobbit");
31+
Category category1 = new Category("Cat1", book1);
32+
categoryRepository.save(category1);
33+
34+
Book book2 = new Book("All Quiet on the Western Front");
35+
Category category2 = new Category("Cat2", book2);
36+
categoryRepository.save(category2);
37+
}
38+
39+
@After
40+
public void teardown() {
41+
bookRepository.deleteAll();
42+
categoryRepository.deleteAll();
43+
}
44+
45+
@Test
46+
public void whenDeletingCategories_thenBooksShouldAlsoBeDeleted() {
47+
categoryRepository.deleteAll();
48+
49+
assertThat(bookRepository.count() == 0).isTrue();
50+
assertThat(categoryRepository.count() == 0).isTrue();
51+
}
52+
53+
@Test
54+
public void whenDeletingBooks_thenCategoriesShouldAlsoBeDeleted() {
55+
bookRepository.deleteAll();
56+
57+
assertThat(bookRepository.count() == 0).isTrue();
58+
assertThat(categoryRepository.count() == 2).isTrue();
59+
}
60+
}

0 commit comments

Comments
 (0)