diff --git a/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/search/Student.java b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/search/Student.java new file mode 100644 index 0000000000..1d6eaa3b33 --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/search/Student.java @@ -0,0 +1,75 @@ +package com.baeldung.spring.data.persistence.search; + +import java.util.Objects; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Student { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + + private String name; + private int score; + + public Student() { + } + + public Student(String name, int score) { + + this.name = name; + this.score = score; + } + + 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 int getScore() { + return score; + } + + public void setScore(int score) { + this.score = score; + } + + @Override + public String toString() { + return "Student [id=" + id + ", name=" + name + ", score=" + score + "]"; + } + + @Override + public int hashCode() { + return Objects.hash(id, name, score); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Student other = (Student) obj; + return id == other.id && Objects.equals(name, other.name) && score == other.score; + } + +} \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/search/StudentApplication.java b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/search/StudentApplication.java new file mode 100644 index 0000000000..6aa895d067 --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/search/StudentApplication.java @@ -0,0 +1,16 @@ +package com.baeldung.spring.data.persistence.search; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class StudentApplication { + + private static final Logger log = LoggerFactory.getLogger(StudentApplication.class); + + public static void main(String[] args) { + SpringApplication.run(StudentApplication.class, args); + } +} \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/search/StudentRepository.java b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/search/StudentRepository.java new file mode 100644 index 0000000000..29aade5886 --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/search/StudentRepository.java @@ -0,0 +1,31 @@ +package com.baeldung.spring.data.persistence.search; + +import java.util.List; + +import org.springframework.data.domain.Sort; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface StudentRepository extends JpaRepository { + + Student findFirstByOrderByScoreDesc(); + + Student findFirstBy(Sort sort); + + Student findFirstByNameLike(String name, Sort sort); + + List findFirst3ByOrderByScoreDesc(); + + List findFirst2ByScoreBetween(int startScore, int endScore, Sort sort); + + Student findTopByOrderByScoreDesc(); + + Student findTopBy(Sort sort); + + Student findTopByNameLike(String name, Sort sort); + + List findTop3ByOrderByScoreDesc(); + + List findTop2ByScoreBetween(int startScore, int endScore, Sort sort); +} diff --git a/persistence-modules/spring-data-jpa-repo-2/src/main/resources/application.properties b/persistence-modules/spring-data-jpa-repo-2/src/main/resources/application.properties index 30cc5abbcc..3ca0cc1242 100644 --- a/persistence-modules/spring-data-jpa-repo-2/src/main/resources/application.properties +++ b/persistence-modules/spring-data-jpa-repo-2/src/main/resources/application.properties @@ -3,3 +3,4 @@ spring.datasource.username=sa spring.datasource.password=sa spring.jpa.properties.hibernate.globally_quoted_identifiers=true +logging.level.com.baeldung.spring.data.persistence.search=debug \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-repo-2/src/test/java/com/baeldung/spring/data/persistence/search/StudentApplicationUnitTest.java b/persistence-modules/spring-data-jpa-repo-2/src/test/java/com/baeldung/spring/data/persistence/search/StudentApplicationUnitTest.java new file mode 100644 index 0000000000..ea16b3597d --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-2/src/test/java/com/baeldung/spring/data/persistence/search/StudentApplicationUnitTest.java @@ -0,0 +1,100 @@ +package com.baeldung.spring.data.persistence.search; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Random; +import java.util.stream.Collectors; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.domain.Sort; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class StudentApplicationUnitTest { + + @Autowired + private StudentRepository studentRepo; + private List students; + + @Before + public void fillData() { + students = new ArrayList<>(); + int count = 10; + Random r = new Random(); + List scores = r.ints(0, 101) + .distinct() + .limit(count) + .boxed() + .collect(Collectors.toList()); + + for (int i = 0; i < count; i++) { + Integer score = scores.get(i); + Student s = new Student("Student-" + i, score); + students.add(s); + } + + studentRepo.saveAll(students); + Comparator c = Comparator.comparing(a -> a.getScore()); + c = c.reversed(); + students.sort(c); + } + + @After + public void clearData() { + studentRepo.deleteAll(); + } + + @Test + public void givenStudentScores_whenMoreThanOne_thenFindFirst() { + + Student student = studentRepo.findFirstByOrderByScoreDesc(); + Student s = students.get(0); + assertEquals(student, s); + } + + @Test + public void givenStudentScores_whenMoreThan3_thenFindFirstThree() { + + List firstThree = studentRepo.findFirst3ByOrderByScoreDesc(); + List sList = students.subList(0, 3); + assertArrayEquals(firstThree.toArray(), sList.toArray()); + } + + @Test + public void givenStudentScores_whenNameMatches_thenFindFirstStudent() { + + String matchString = "3"; + Student student = studentRepo.findFirstByNameLike("%" + matchString + "%", Sort.by("score") + .descending()); + Student s = students.stream() + .filter(a -> a.getName() + .contains(matchString)) + .findFirst() + .orElse(null); + assertEquals(student, s); + } + + @Test + public void givenStudentScores_whenBetweenRange_thenFindFirstTwoStudents() { + + List topTwoBetweenRange = studentRepo.findFirst2ByScoreBetween(50, 60, Sort.by("score") + .descending()); + List _students = students.stream() + .filter(a -> a.getScore() >= 50 && a.getScore() <= 60) + .limit(2) + .collect(Collectors.toList()); + assertArrayEquals(_students.toArray(), topTwoBetweenRange.toArray()); + } +}