"first commit in new fork"

This commit is contained in:
Arindum Roy 2020-04-19 21:46:19 +05:30
parent 87ac7a74be
commit c4ed1770e2
5 changed files with 189 additions and 0 deletions

View File

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

View File

@ -0,0 +1,47 @@
package com.baeldung.composite.entity;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
@Entity
public class Book {
@EmbeddedId
private BookId id;
private String genre;
private Integer price;
public Book() {
}
public Book(String author, String name, String genre, Integer price) {
BookId id = new BookId(author, name);
this.id = id;
this.genre = genre;
this.price = price;
}
public BookId getId() {
return id;
}
public void setId(BookId id) {
this.id = id;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
}

View File

@ -0,0 +1,51 @@
package com.baeldung.composite.entity;
import javax.persistence.Embeddable;
import java.io.Serializable;
import java.util.Objects;
@Embeddable
public class BookId implements Serializable {
private String author;
private String name;
public BookId() {
}
public BookId(String author, String name) {
this.author = author;
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
BookId bookId = (BookId) o;
return Objects.equals(author, bookId.author) && Objects.equals(name, bookId.name);
}
@Override
public int hashCode() {
return Objects.hash(author, name);
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.composite.repository;
import com.baeldung.composite.entity.Book;
import com.baeldung.composite.entity.BookId;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface BookRepository extends JpaRepository<Book, BookId> {
List<Book> findByIdName(String name);
List<Book> findByIdAuthor(String author);
}

View File

@ -0,0 +1,63 @@
package com.baeldung.composite.repository;
import com.baeldung.composite.BookApplication;
import com.baeldung.composite.entity.Book;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
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 java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertEquals;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = BookApplication.class)
public class BookRepositoryTest {
public static final String JAVA_101 = "Java101";
public static final String JANE = "Jane";
@Autowired
BookRepository repository;
@Before
public void setUp() {
Book book1 = new Book("John", JAVA_101, "Tech", 20);
Book book2 = new Book(JANE, JAVA_101, "Arch", 25);
Book book3 = new Book(JANE, "Scala101", "Tech", 23);
repository.saveAll(Arrays.asList(book1, book2, book3));
}
@After
public void tearDown() {
repository.deleteAll();
}
@Test
public void testFindByName() {
List<Book> books = repository.findByIdName(JAVA_101);
assertEquals(2, books.size());
}
@Test
public void testFindByAuthor() {
List<Book> books = repository.findByIdAuthor(JANE);
assertEquals(2, books.size());
}
@Test
public void testFindByGenre() {
List<Book> books = repository.findByIdAuthor(JANE);
assertEquals(2, books.size());
}
}