BAEL-6119: Difference Between findBy and findAllBy in Spring Data JPA (#13344)
* player entity and repository. test structure * moving modules * test findFirst. cleanups. * adding @Repository * player entity and repository. test structure * moving modules * test findFirst. cleanups. * adding @Repository * changing module * adding show-sql config * removing unrelated change * properties SB annotation
This commit is contained in:
parent
6a7adf631d
commit
bfb2b1b9f4
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<Player, Long> {
|
||||
List<Player> findByScoreGreaterThan(Integer target);
|
||||
|
||||
List<Player> findAllByScoreGreaterThan(Integer target);
|
||||
|
||||
Optional<Player> findFirstByScoreGreaterThan(Integer target);
|
||||
}
|
|
@ -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<Player> findByPlayers = playerRepository.findByScoreGreaterThan(400);
|
||||
List<Player> findAllByPlayers = playerRepository.findAllByScoreGreaterThan(400);
|
||||
assertEquals(findByPlayers, findAllByPlayers);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSavedPlayer_whenUseFindFirst_thenReturnSingleResult() {
|
||||
Optional<Player> player = playerRepository.findFirstByScoreGreaterThan(400);
|
||||
assertTrue(player.isPresent());
|
||||
assertEquals(600, player.get().getScore());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue