BAEL-2810: Finished the examples and unit tests. (#6732)
This commit is contained in:
parent
2cc1de2e39
commit
6796ec3af7
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
||||
}
|
|
@ -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> {
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue