forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-2810: Finished the examples and unit tests. (eugenp#6732)
- Loading branch information
1 parent
2cc1de2
commit 6796ec3
Showing
6 changed files
with
271 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
...tence-modules/spring-data-jpa-2/src/main/java/com/baeldung/datajpadelete/entity/Book.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.baeldung.datajpadelete.entity; | ||
|
||
import javax.persistence.*; | ||
|
||
@Entity | ||
public class Book { | ||
|
||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
private String title; | ||
|
||
@ManyToOne | ||
private Category category; | ||
|
||
public Book() { | ||
} | ||
|
||
public Book(String title) { | ||
this.title = title; | ||
} | ||
|
||
public Book(String title, Category category) { | ||
this.title = title; | ||
this.category = category; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public Category getCategory() { | ||
return category; | ||
} | ||
|
||
public void setCategory(Category category) { | ||
this.category = category; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...e-modules/spring-data-jpa-2/src/main/java/com/baeldung/datajpadelete/entity/Category.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.baeldung.datajpadelete.entity; | ||
|
||
import javax.persistence.*; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
@Entity | ||
public class Category { | ||
|
||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
private String name; | ||
|
||
@OneToMany(mappedBy = "category", cascade = CascadeType.ALL, orphanRemoval = true) | ||
private List<Book> books; | ||
|
||
public Category() { | ||
} | ||
|
||
public Category(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Category(String name, Book... books) { | ||
this.name = name; | ||
this.books = Stream.of(books).collect(Collectors.toList()); | ||
this.books.forEach(x -> x.setCategory(this)); | ||
} | ||
|
||
public Category(String name, List<Book> books) { | ||
this.name = name; | ||
this.books = books; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public List<Book> getBooks() { | ||
return books; | ||
} | ||
|
||
public void setBooks(List<Book> books) { | ||
this.books = books; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...spring-data-jpa-2/src/main/java/com/baeldung/datajpadelete/repository/BookRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.baeldung.datajpadelete.repository; | ||
|
||
import com.baeldung.datajpadelete.entity.Book; | ||
import org.springframework.data.jpa.repository.Modifying; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.data.repository.query.Param; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface BookRepository extends CrudRepository<Book, Long> { | ||
|
||
long deleteByTitle(String title); | ||
|
||
@Modifying | ||
@Query("delete from Book b where b.title=:title") | ||
void deleteBooks(@Param("title") String title); | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
...ng-data-jpa-2/src/main/java/com/baeldung/datajpadelete/repository/CategoryRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.baeldung.datajpadelete.repository; | ||
|
||
import com.baeldung.datajpadelete.entity.Category; | ||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface CategoryRepository extends CrudRepository<Category, Long> { | ||
} |
72 changes: 72 additions & 0 deletions
72
...ing-data-jpa-2/src/test/java/com/baeldung/datajpadelete/DeleteFromRepositoryUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.baeldung.datajpadelete; | ||
|
||
import com.baeldung.Application; | ||
import com.baeldung.datajpadelete.entity.Book; | ||
import com.baeldung.datajpadelete.repository.BookRepository; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Arrays; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes = {Application.class}) | ||
public class DeleteFromRepositoryUnitTest { | ||
|
||
@Autowired | ||
private BookRepository repository; | ||
|
||
Book book1; | ||
Book book2; | ||
|
||
@Before | ||
public void setup() { | ||
book1 = new Book("The Hobbit"); | ||
book2 = new Book("All Quiet on the Western Front"); | ||
|
||
repository.saveAll(Arrays.asList(book1, book2)); | ||
} | ||
|
||
@After | ||
public void teardown() { | ||
repository.deleteAll(); | ||
} | ||
|
||
@Test | ||
public void whenDeleteByIdFromRepository_thenDeletingShouldBeSuccessful() { | ||
repository.deleteById(book1.getId()); | ||
|
||
assertThat(repository.count() == 1).isTrue(); | ||
} | ||
|
||
@Test | ||
public void whenDeleteAllFromRepository_thenRepositoryShouldBeEmpty() { | ||
repository.deleteAll(); | ||
|
||
assertThat(repository.count() == 0).isTrue(); | ||
} | ||
|
||
@Test | ||
@Transactional | ||
public void whenDeleteFromDerivedQuery_thenDeletingShouldBeSuccessful() { | ||
repository.deleteByTitle("The Hobbit"); | ||
|
||
assertThat(repository.count() == 1).isTrue(); | ||
} | ||
|
||
@Test | ||
@Transactional | ||
public void whenDeleteFromCustomQuery_thenDeletingShouldBeSuccessful() { | ||
repository.deleteBooks("The Hobbit"); | ||
|
||
assertThat(repository.count() == 1).isTrue(); | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
...ng-data-jpa-2/src/test/java/com/baeldung/datajpadelete/DeleteInRelationshipsUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.baeldung.datajpadelete; | ||
|
||
import com.baeldung.Application; | ||
import com.baeldung.datajpadelete.entity.Book; | ||
import com.baeldung.datajpadelete.entity.Category; | ||
import com.baeldung.datajpadelete.repository.BookRepository; | ||
import com.baeldung.datajpadelete.repository.CategoryRepository; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes = {Application.class}) | ||
public class DeleteInRelationshipsUnitTest { | ||
|
||
@Autowired | ||
private BookRepository bookRepository; | ||
|
||
@Autowired | ||
private CategoryRepository categoryRepository; | ||
|
||
@Before | ||
public void setup() { | ||
Book book1 = new Book("The Hobbit"); | ||
Category category1 = new Category("Cat1", book1); | ||
categoryRepository.save(category1); | ||
|
||
Book book2 = new Book("All Quiet on the Western Front"); | ||
Category category2 = new Category("Cat2", book2); | ||
categoryRepository.save(category2); | ||
} | ||
|
||
@After | ||
public void teardown() { | ||
bookRepository.deleteAll(); | ||
categoryRepository.deleteAll(); | ||
} | ||
|
||
@Test | ||
public void whenDeletingCategories_thenBooksShouldAlsoBeDeleted() { | ||
categoryRepository.deleteAll(); | ||
|
||
assertThat(bookRepository.count() == 0).isTrue(); | ||
assertThat(categoryRepository.count() == 0).isTrue(); | ||
} | ||
|
||
@Test | ||
public void whenDeletingBooks_thenCategoriesShouldAlsoBeDeleted() { | ||
bookRepository.deleteAll(); | ||
|
||
assertThat(bookRepository.count() == 0).isTrue(); | ||
assertThat(categoryRepository.count() == 2).isTrue(); | ||
} | ||
} |