Add code from article (#10552)

This commit is contained in:
Jordan Simpson 2021-03-14 15:18:13 -05:00 committed by GitHub
parent 18bd57a704
commit af0b504061
6 changed files with 132 additions and 0 deletions

View File

@ -0,0 +1,39 @@
package com.baeldung.boot.readonlyrepository;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Book
{
@Id
@GeneratedValue
private Long id;
private String author;
private String title;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.boot.readonlyrepository;
import java.util.List;
public interface BookReadOnlyRepository extends ReadOnlyRepository<Book, Long> {
List<Book> findByAuthor(String author);
List<Book> findByTitle(String title);
}

View File

@ -0,0 +1,15 @@
package com.baeldung.boot.readonlyrepository;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.Repository;
import java.util.List;
import java.util.Optional;
@NoRepositoryBean
public interface ReadOnlyRepository<T, ID> extends Repository<T, ID> {
Optional<T> findById(ID id);
List<T> findAll();
}

View File

@ -0,0 +1,12 @@
package com.baeldung.boot.readonlyrepository;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ReadOnlyRepositoryApplication {
public static void main(String[] args) {
SpringApplication.run(ReadOnlyRepositoryApplication.class, args);
}
}

View File

@ -0,0 +1,6 @@
package com.baeldung.boot.readonlyrepository;
import org.springframework.data.repository.CrudRepository;
public interface BookRepository extends BookReadOnlyRepository, CrudRepository<Book, Long> {
}

View File

@ -0,0 +1,50 @@
package com.baeldung.boot.readonlyrepository;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
import java.util.NoSuchElementException;
@SpringBootTest( classes = ReadOnlyRepositoryApplication.class )
public class ReadOnlyRepositoryUnitTest
{
@Autowired
private BookRepository bookRepository;
@Autowired
private BookReadOnlyRepository bookReadOnlyRepository;
@Test
public void givenBooks_whenUsingReadOnlyRepository_thenGetThem() {
Book aChristmasCarolCharlesDickens = new Book();
aChristmasCarolCharlesDickens.setTitle("A Christmas Carol");
aChristmasCarolCharlesDickens.setAuthor("Charles Dickens");
bookRepository.save(aChristmasCarolCharlesDickens);
Book greatExpectationsCharlesDickens = new Book();
greatExpectationsCharlesDickens.setTitle("Great Expectations");
greatExpectationsCharlesDickens.setAuthor("Charles Dickens");
bookRepository.save(greatExpectationsCharlesDickens);
Book greatExpectationsKathyAcker = new Book();
greatExpectationsKathyAcker.setTitle("Great Expectations");
greatExpectationsKathyAcker.setAuthor("Kathy Acker");
bookRepository.save(greatExpectationsKathyAcker);
List<Book> charlesDickensBooks = bookReadOnlyRepository.findByAuthor("Charles Dickens");
Assertions.assertEquals(2, charlesDickensBooks.size());
List<Book> greatExpectationsBooks = bookReadOnlyRepository.findByTitle("Great Expectations");
Assertions.assertEquals(2, greatExpectationsBooks.size());
List<Book> allBooks = bookReadOnlyRepository.findAll();
Assertions.assertEquals(3, allBooks.size());
Long bookId = allBooks.get(0).getId();
Book book = bookReadOnlyRepository.findById(bookId).orElseThrow(NoSuchElementException::new);
Assertions.assertNotNull(book);
}
}