I have this service method which calls a repo method which is in turn generated by spring:
Service method:
public User findUserByEmail(String email) {
try {
Assert.notNull(email,"email is null!");
Optional<User> userOptional = userRepo.findUserDAOByEmail(email);
return userOptional.orElseThrow();
}
catch (...){
...
}
}
Repo:
public interface UserRepo extends JpaRepository<User, Long> {
Optional<User> findUserByEmail(String email);
}
I wanted the repo method to throw IllegalArgumentException as its friends (the methods that come out of the box with Jparepository do), but instead it accepted the null parameter "email" and searched for it in the database and returned a result that no such user exist (p.s.: email is constrained to be not null in my schema)
So I did Assert.notNull(email,"email is null!");
But I wonder if there is a better way to validate that "email" is not null?