diff --git a/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/findbyvsfindallby/FindByVsFindAllByApplication.java b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/findbyvsfindallby/FindByVsFindAllByApplication.java new file mode 100644 index 0000000000..c9757e2f04 --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/findbyvsfindallby/FindByVsFindAllByApplication.java @@ -0,0 +1,11 @@ +package com.baeldung.spring.data.persistence.findbyvsfindallby; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class FindByVsFindAllByApplication { + public static void main(String[] args) { + SpringApplication.run(FindByVsFindAllByApplication.class, args); + } +} diff --git a/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/findbyvsfindallby/model/Player.java b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/findbyvsfindallby/model/Player.java new file mode 100644 index 0000000000..0d8f833b4c --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/findbyvsfindallby/model/Player.java @@ -0,0 +1,43 @@ +package com.baeldung.spring.data.persistence.findbyvsfindallby.model; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import java.util.Objects; + +@Entity +public class Player { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + + private Integer score; + + public Player(Integer score) { + this.score = score; + } + + public Player() { + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public Integer getScore() { + return score; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Player player = (Player) o; + return id == player.id && Objects.equals(score, player.score); + } +} diff --git a/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/findbyvsfindallby/repository/PlayerRepository.java b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/findbyvsfindallby/repository/PlayerRepository.java new file mode 100644 index 0000000000..6b0d1c6e5d --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-2/src/main/java/com/baeldung/spring/data/persistence/findbyvsfindallby/repository/PlayerRepository.java @@ -0,0 +1,17 @@ +package com.baeldung.spring.data.persistence.findbyvsfindallby.repository; + +import com.baeldung.spring.data.persistence.findbyvsfindallby.model.Player; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Optional; + +@Repository +public interface PlayerRepository extends JpaRepository { + List findByScoreGreaterThan(Integer target); + + List findAllByScoreGreaterThan(Integer target); + + Optional findFirstByScoreGreaterThan(Integer target); +} diff --git a/persistence-modules/spring-data-jpa-repo-2/src/test/java/com/baeldung/spring/data/persistence/findbyvsfindallby/FindByVsFindAllByIntegrationTest.java b/persistence-modules/spring-data-jpa-repo-2/src/test/java/com/baeldung/spring/data/persistence/findbyvsfindallby/FindByVsFindAllByIntegrationTest.java new file mode 100644 index 0000000000..add340b4dd --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-2/src/test/java/com/baeldung/spring/data/persistence/findbyvsfindallby/FindByVsFindAllByIntegrationTest.java @@ -0,0 +1,46 @@ +package com.baeldung.spring.data.persistence.findbyvsfindallby; + +import com.baeldung.spring.data.persistence.findbyvsfindallby.model.Player; +import com.baeldung.spring.data.persistence.findbyvsfindallby.repository.PlayerRepository; +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 java.util.Arrays; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = FindByVsFindAllByApplication.class, properties = "spring.jpa.show-sql=true") +public class FindByVsFindAllByIntegrationTest { + @Autowired + private PlayerRepository playerRepository; + + @Before + public void setup() { + Player player1 = new Player(600); + Player player2 = new Player(500); + Player player3 = new Player(300); + playerRepository.saveAll(Arrays.asList(player1, player2, player3)); + } + + @Test + public void givenSavedPlayer_whenUseFindByOrFindAllBy_thenReturnSameResult() { + List findByPlayers = playerRepository.findByScoreGreaterThan(400); + List findAllByPlayers = playerRepository.findAllByScoreGreaterThan(400); + assertEquals(findByPlayers, findAllByPlayers); + } + + @Test + public void givenSavedPlayer_whenUseFindFirst_thenReturnSingleResult() { + Optional player = playerRepository.findFirstByScoreGreaterThan(400); + assertTrue(player.isPresent()); + assertEquals(600, player.get().getScore()); + } +}