I'm testing my jpa codes with junit, which using ExampleMatcher.
This code should filter users with email by matching end of it.
Here's my code about domain object(User)
@Data
@AllArgsConstructor
@NoArgsConstructor
@RequiredArgsConstructor
@Entity
@Table(name = "myuser")
public class User {
@Id
@GeneratedValue
private long id;
@NonNull
private String name;
@NonNull
private String email;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
}
And I tested filtering with my code below.
@Test
void crud7(){
userRepository.save(new User("hyechan", "hyechan1108@google.com"));
userRepository.save(new User("steve", "steve4234@google.com"));
userRepository.save(new User("roger", "roger11@naver.com"));
ExampleMatcher matcher = ExampleMatcher.matching()
.withIgnorePaths("name")
.withMatcher("email", endsWith());
Example<User> example = Example.of(new User("ma", "google.com"), matcher);
userRepository.findAll(example).forEach(System.out::println);
}
I thought this code would work fine, but no matching data was found.
I've also made sure that three user data are properly been saved.
Also, I found that Hibernate log contains code about matching primary key in query.
Hibernate:
select
u1_0.id,
u1_0.created_at,
u1_0.email,
u1_0.name,
u1_0.updated_at
from
myuser u1_0
where
u1_0.id=?
and u1_0.email like ? escape '\'
I don't know why that happened, but when I fix my filtering code like below, it works fine as I want.
I added code to ignore primary key (.withIgnorePaths("id")
)
@Test
void crud7(){
userRepository.save(new User("hyechan", "hyechan1108@google.com"));
userRepository.save(new User("steve", "steve4234@google.com"));
userRepository.save(new User("roger", "roger11@naver.com"));
ExampleMatcher matcher = ExampleMatcher.matching()
.withIgnorePaths("name")
.withIgnorePaths("id")
.withMatcher("email", endsWith());
Example<User> example = Example.of(new User("ma", "google.com"), matcher);
userRepository.findAll(example).forEach(System.out::println);
}
So, does ExampleMatcher works with primary key by default, or am I missing something?
FYI) Release Version
id 'org.springframework.boot' version '3.0.2'
com.h2database:h2:2.1.214