Home > database >  Spring Boot JPA not running JPQL Query
Spring Boot JPA not running JPQL Query

Time:08-07

In spring boot, JPA query isn't returning anything. Console doesn't show any errors. The save and findAll() methods are running fine, only the QUERY method is failing.

The database has data, in running the getAdminByUsername() to get username and password for a different class.

After trying multiple sysout statement i've noticed that the control stops at AdminInfo admin = adminRepository.getByUsername(username); in controller

I've checked and made sure that username column and the entry exists in the database.

Controller :

    @RestController
    public class AdminController {

    @Autowired
    private AdminRepository adminRepository;
    
    
    
    public AdminInfo getAdminByUsername(String username) {
        AdminInfo admin = adminRepository.getByUsername(username);
        return admin;
    }

Repository :

package com.boot.insurance.main.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import com.boot.insurance.main.model.AdminInfo;

public interface AdminRepository extends JpaRepository<AdminInfo, Long>{

    @Query("select a from AdminInfo a where a.username=?1")
    AdminInfo getByUsername(String username);

}

Admin Class:

package com.boot.insurance.main.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "staff")
public class AdminInfo {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    @Column(nullable = false)
    private String username;


    @Column(nullable = false)
    private String name;
    
    @Column(nullable = false)
    private String password;

    public AdminInfo() {
        super();
        // TODO Auto-generated constructor stub
    }


    public AdminInfo(Long id, String username, String name, String password) {
        super();
        this.id = id;
        this.username = username;
        this.name = name;
        this.password = password;
    }


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "AdminInfo [id="   id   ", username="   username   ", name="   name   ", password="   password   "]";
    }
    
}

CodePudding user response:

Try change

  @RestController
    public class AdminController {

    @Autowired
    private AdminRepository adminRepository;
    
    
    
    public AdminInfo getAdminByUsername(String username) {
        AdminInfo admin = adminRepository.getByUsername(username);
        return admin;
    }

to:

    @RequestMapping(value = "/user")
    @RestController
    @CrossOrigin
    public class AdminController {
    
        @Autowired
        private AdminRepository adminRepository;
    
        @GetMapping(value = "/user/{username}")
        public ResponseEntity<AdminInfo> getStringU(@PathVariable("username") String username) {
               return new ResponseEntity<>(adminRepository.getByUsername(username), HttpStatus.OK);
        }
 
   }

You does not pass a parameter to the controller, hence no data, try this should help.

CodePudding user response:

    @RequestMapping(value = "/user")
    @RestController
    public class AdminController {
    
        @Autowired
        private AdminRepository adminRepository;
    
        @GetMapping(value = "/{username}", produces = "application/json")
        public AdminInfo getAdminByUsername(@PathVariable("username") String username) {
               return adminRepository.getByUsername(username);
        }
    }
  • Related