Bael 2826 case insensitive spring data jpa (#6668)

* BAEL-2826: Case Insensitive Comparison Spring data jpa

* BAEL-2826: Fixing imports

* BAEL-2826: Case Insensitive Comparison Spring data jpa

* BAEL-2826: Fixing imports

* BAEL-2826: Moving code to another repo

* BAEL-2826: Moving code to another repo

* BAEL-2826 : Refactor

* BAEL-2826 : Refactor

* BAEL-2826 : Override equals hashcode
This commit is contained in:
Shubhra Srivastava 2019-04-06 21:30:35 +05:30 committed by maibin
parent 250604d1a1
commit 06cb2e5351
3 changed files with 146 additions and 0 deletions

View File

@ -0,0 +1,78 @@
package com.baeldung.entity;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.Objects;
@Entity
public class Passenger {
@Id
@GeneratedValue
@Column(nullable = false)
private Long id;
@Basic(optional = false)
@Column(nullable = false)
private String firstName;
@Basic(optional = false)
@Column(nullable = false)
private String lastName;
private Passenger(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public static Passenger from(String firstName, String lastName) {
return new Passenger(firstName, lastName);
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Passenger{" + "id=" + id + ", firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + '}';
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Passenger passenger = (Passenger) o;
return Objects.equals(firstName, passenger.firstName) && Objects.equals(lastName, passenger.lastName);
}
@Override
public int hashCode() {
return Objects.hash(firstName, lastName);
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.repository;
import com.baeldung.entity.Passenger;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
interface PassengerRepository extends JpaRepository<Passenger, Long> {
List<Passenger> findByFirstNameIgnoreCase(String firstName);
}

View File

@ -0,0 +1,54 @@
package com.baeldung.repository;
import com.baeldung.entity.Passenger;
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.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.core.IsNot.not;
@DataJpaTest
@RunWith(SpringRunner.class)
public class PassengerRepositoryIntegrationTest {
@PersistenceContext
private EntityManager entityManager;
@Autowired
private PassengerRepository repository;
@Before
public void before() {
entityManager.persist(Passenger.from("Jill", "Smith"));
entityManager.persist(Passenger.from("Eve", "Jackson"));
entityManager.persist(Passenger.from("Fred", "Bloggs"));
entityManager.persist(Passenger.from("Ricki", "Bobbie"));
entityManager.persist(Passenger.from("Siya", "Kolisi"));
}
@Test
public void givenPassengers_whenMatchingIgnoreCase_thenExpectedReturned() {
Passenger jill = Passenger.from("Jill", "Smith");
Passenger eve = Passenger.from("Eve", "Jackson");
Passenger fred = Passenger.from("Fred", "Bloggs");
Passenger siya = Passenger.from("Siya", "Kolisi");
Passenger ricki = Passenger.from("Ricki", "Bobbie");
List<Passenger> passengers = repository.findByFirstNameIgnoreCase("FRED");
assertThat(passengers, contains(fred));
assertThat(passengers, not(contains(eve)));
assertThat(passengers, not(contains(siya)));
assertThat(passengers, not(contains(jill)));
assertThat(passengers, not(contains(ricki)));
}
}