abi jega
abi jega

Reputation: 135

Error creating bean with name Spring Boot

I am new to using Spring Boot. I have already got the user registration form and API working fine and successfully added the records into the database. After completing the login API I ran into the problem with the attached error below. I have attached what I have tried below. I want to display if the login was successful. Displayed as a JSON format it looks like this, I need help, please help me to correct the code below

{
  status = "true"'
  message = "login success"
}

Full Error

ework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeController': Unsatisfied dependency expressed through field 'employeeService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeIMPL': Unsatisfied dependency expressed through field 'employeeRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeRepo' defined in com.example.Registation.Repo.EmployeeRepo defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract com.example.Registation.Entity.Employee com.example.Registation.Repo.EmployeeRepo.findByUsernamePassword(java.lang.String,java.lang.String); Reason: Failed to create query for method public abstract com.example.Registation.Entity.Employee com.example.Registation.Repo.EmployeeRepo.findByUsernamePassword(java.lang.String,java.lang.String)! No property 'usernamePassword' found for type 'Employee'; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract com.example.Registation.Entity.Employee com.example.Registation.Repo.EmployeeRepo.findByUsernamePassword(java.lang.String,java.lang.String)! No property 'usernamePassword' found for type 'Employee'
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:659) ~[spring-beans-5.3.23.jar:5.3.23]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639) ~[spring-beans-5.3.23.jar:5.3.23]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.23.jar:5.3.23]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.23.jar:5.3.23]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431) ~[spring-beans-5.3.23.jar:5.3.23]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619) ~[spring-beans-5.3.23.jar:5.3.23]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.23.jar:5.3.23]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.23.jar:5.3.23]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.23.jar:5.3.23]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.23.jar:5.3.23]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.23.jar:5.3.23]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListabl

LoginController

  @PostMapping(path = "/login")
    public String saveEmployee(@RequestBody LoginDTO loginDTO)
    {
        String name = employeeService.loginEmployee(loginDTO);
       return "success";
    }

LoginDTO.java

public class LoginDTO {
    private String email;
    private String password;

    public LoginDTO() {
    }

    public LoginDTO(String email, String password) {
        this.email = email;
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

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

    @Override
    public String toString() {
        return "LoginDTO{" +
                "email='" + email + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

Employee.java

@Entity
@Table(name="employee")
public class Employee {

    @Id
    @Column(name="employee_id", length = 45)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int employeeid;

    @Column(name="employee_name", length = 255)
    private String employeename;

    @Column(name="email", length = 255)
    private String email;

    @Column(name="password", length = 255)
    private String password;


    public Employee() {
    }

    public Employee(int employeeid, String employeename, String email, String password) {
        this.employeeid = employeeid;
        this.employeename = employeename;
        this.email = email;
        this.password = password;
    }


    public int getEmployeeid() {
        return employeeid;
    }

    public void setEmployeeid(int employeeid) {
        this.employeeid = employeeid;
    }

    public String getEmployeename() {
        return employeename;
    }

    public void setEmployeename(String employeename) {
        this.employeename = employeename;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

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

    @Override
    public String toString() {
        return "Employee{" +
                "employeeid=" + employeeid +
                ", employeename='" + employeename + '\'' +
                ", email='" + email + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

Repo

@EnableJpaRepositories
@Repository
public interface EmployeeRepo extends JpaRepository<Employee,Integer> {


      public  Employee findByUsernamePassword(String username, String password);


}

EmployeeService.java

public interface EmployeeService {
    String addEmployee(EmployeeDTO employeeDTO);

    String loginEmployee(LoginDTO loginDTO);
}

EmployeeIMPL.java

public String loginEmployee(LoginDTO loginDTO) {
  Employee employee = employeeRepo.findByUsernamePassword(loginDTO.getEmail(),loginDTO.getPassword());

  if(employee == null)
  {
      return "Employee ID Not Found";
  }
  else
  {
      return "Logged In";
  }
}

Upvotes: 1

Views: 1962

Answers (1)

abetobing
abetobing

Reputation: 191

The error message is pretty clear. No property 'usernamePassword' found for type 'Employee'

It expect usernamePassword field inside Employee, but in your Employee.java you have email and password?

You might want to change findByUsernamePassword(String username, String password); to:

public  Employee findOneByEmailAndPassword(String email, String password);

Or

Create a String username field in Employee.java with its getters and setters. Then change the repository method to

public  Employee findOneByUsernameAndPassword(String username, String password);

Read this for more details https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation

Upvotes: 3

Related Questions