Home > Software design >  JPARepository findByUserName method returns null values
JPARepository findByUserName method returns null values

Time:11-16

I want to use Spring security to get the currently logged in user object but it returns a null value. I implemented the findByUsername method from the repository to test but it comes back with null/empty values and I've ensured data is in the database. What could I be doing wrong ?

Entity class

public class User {

    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;
    private String firstName ;
    private String lastName;
    @Column(name="user_name", unique=true)
    private String userName;
    private String password;
    private String Gender; 

Repository class

@Repository
public interface UserAccountRepository extends JpaRepository <User, Long>  {
    
     Optional<User> findById(Long id);

     User findByUserName(String username);

Service class

@Transactional
@Service
public class UserAccountService implements UserDetailsService {

        @Autowired  
        private UserAccountRepository userRepository;
        
        private PasswordEncoder bCryptPasswordEncoder;
     
        public User findByUserName(String username) 
        {
        return userRepository.findByUserName(username);
        }

Controller class

     @GetMapping(value="/user/{username}")
        public User findByUsername(String username) {
          System.out.println("Username :"   username);
          return userAccountService.findByUserName(username);
            
        }

Application properties

spring.datasource.url=jdbc:mysql://localhost:3306/investmentpartners
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username= root
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=update
## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

spring.main.allow-bean-definition-overriding=true

spring.servlet.multipart.max-file-size=3000KB
spring.servlet.multipart.max-request-size=3000KB
spring.http.multipart.enabled=false

After adding spring.jpa.show-sql=true to application.properties file here is the query generated

Hibernate: select user0_.id as id1_4_, user0_.gender as gender2_4_, user0_.branch_id as branch_11_4_, user0_.created_date as created_3_4_, user0_.email as email4_4_, user0_.first_name as first_na5_4_, user0_.last_name as last_nam6_4_, user0_.password as password7_4_, user0_.phone_number as phone_nu8_4_, user0_.status as status9_4_, user0_.user_name as user_na10_4_ from user user0_ where user0_.user_name is null
null

CodePudding user response:

You have forgotten to mark your path variable with annotation.

Solution:

@PathVariable String username

Controller class

@GetMapping(value="/user/{username}")
public User findByUsername(@PathVariable String username) {
   System.out.println("Username :"   username);
   return userAccountService.findByUserName(username);
}

Note:

Do not use System.out.println in your code to log out something, especially if you use Spring and your code runs in a server (Tomcat). Spring offers a perfect, easy to use way to write something to log:

public class YourController {

    Logger logger = LoggerFactory.getLogger(YourController .class);

    @GetMapping(...)
    public String index(...) {
        logger.debug("A DEBUG Message");
        logger.info("An INFO Message");
        // ...
    }
}
  • Related