Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data JPA query returns null

My project based on spring boot,Thymeleaf,mysql,html and Jquery.

i wrote a query for checking user name and password is valid or not,if valid means return TRUE otherwise false..This is my scenario..but it passing null...so it becomes nullpointer exception.. Here is my code

public interface RepoUserSignup extends JpaRepository<EntUserSignup, Integer> 
{

    @Query("SELECT pk FROM EntUserSignup pk WHERE pk.username=:uname AND pk.password=:pwd")
    Boolean checkUsername(@Param("uname") String username,@Param("pwd") String password);


}

Please help me..Thanks in advance

like image 241
Vignesh R Avatar asked Oct 28 '25 01:10

Vignesh R


1 Answers

Replace your method with this:

Optional<EntUserSignup> findByUsernameAndPassword(String username, String password);

Then in your business layer you can do something like this:

EntUserSignup user = findByUsernameAndPassword(username, password)
                     .orElseThrow(() -> new UsernameNotFoundException("User not found!"));

And of cause don't forget about password in plain text...


A good tutorial how to implement security in Spring Boot application...

like image 128
Cepr0 Avatar answered Oct 29 '25 18:10

Cepr0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!