all files

This commit is contained in:
Arindum Roy 2020-05-02 00:23:59 +05:30
parent 5bfa01ec05
commit 8ef7cc6b81
9 changed files with 254 additions and 0 deletions

View File

@ -0,0 +1,11 @@
### Relevant Articles:
- [Derived Query Methods in Spring Data JPA Repositories](TBD)
### Eclipse Config
After importing the project into Eclipse, you may see the following error:
"No persistence xml file found in project"
This can be ignored:
- Project -> Properties -> Java Persistance -> JPA -> Error/Warnings -> Select Ignore on "No persistence xml file found in project"
Or:
- Eclipse -> Preferences - Validation - disable the "Build" execution of the JPA Validator

View File

@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-data-jpa-5</artifactId>
<name>spring-data-jpa-5</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
</project>

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,18 @@
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);
List<Book> findByGenre(String genre);
}

View File

@ -0,0 +1,2 @@
spring.jpa.show-sql=true

View File

@ -0,0 +1,64 @@
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 BookRepositoryIntegrationTest {
public static final String JAVA_101 = "Java101";
public static final String JANE = "Jane";
public static final String TECH = "Tech";
@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.findByGenre(TECH);
assertEquals(2, books.size());
}
}

View File

@ -0,0 +1,2 @@
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:h2:mem:jpa3