0

Unsatisfied dependency expressed through field 'userRepository'

Can anyone tell me what I'm missing or need to add? Thanks a lot in advance!

Here's a picture of my project structure.

User.java

package org.codigo.entites;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class User {

@Id
@GeneratedValue
private Long id;
private String fname;
private String lname;
public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}
public String getFname() {
    return fname;
}
public void setFname(String fname) {
    this.fname = fname;
}
public String getLname() {
    return lname;
}
public void setLname(String lname) {
    this.lname = lname;
}
public User(String fname, String lname) {
    this.fname = fname;
    this.lname = lname;
}
public User() {
}
@Override
public String toString() {
    return "User [id=" + id + ", fname=" + fname + ", lname=" + lname + "]";
}
}

UserRepository.java

package org.codigo.repositories;

import org.codigo.entites.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {

}
Devmix
  • 1,599
  • 5
  • 36
  • 73

2 Answers2

2

Your UserRepository is interface, you cannot create bean for interface

public interface UserRepository extends JpaRepository<User, Long>

Declare UserRepository as class with method implementations and annotate with @Repository annotation, if JpaRepository is interface then you should use implements keyword

@Repository
public class UserRepository extends JpaRepository<User, Long>
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • It worked! thank you. Quick question: Do you why findOne and delete dont work with my version of spring-boot? I commented those methods out, but I want to use them but I get an error. Do you know what can be causing the error? – Devmix Oct 03 '18 at 00:27
  • Yes, those are built-in methods . They are inside UserController Class – Devmix Oct 03 '18 at 00:31
  • In the class `UserRepository` you need to provide implementation for those methods, `JpaRepository` is an interface ? – Ryuzaki L Oct 03 '18 at 00:31
  • Or do you know another way of finding a user by his ID and deleting a user by his ID using spring – Devmix Oct 03 '18 at 00:31
  • can you update your `JpaRepository` code in post?, if it is an interface you need to implement methods of interface in implemented class https://docs.oracle.com/javase/tutorial/java/IandI/usinginterface.html – Ryuzaki L Oct 03 '18 at 00:34
1

Whenever Spring says Unsatisfied dependency, it does not know how to obtain an instance of the desired type.

To let Spring know and even implement your repository (just the interface), you should

add @EnableJpaRepositories to MyAppApplication:

@SpringBootApplication
@EnableJpaRepositories(basePackage = "org.codigo.repositories")
public class MyAppApplication implements CommandLineRunner { ... }

This seems like magic at first - behind the scenes Spring will instantiate a dynamic proxy for your repository which intercepts all method calls and finds out what to do, based on the called method's name, its parameters and return type.

Reference:

Hero Wanders
  • 3,237
  • 1
  • 10
  • 14
  • Voting it up now. Quick question: Wasn't the Repository annotation supposed to be ONLY for classes? – Devmix Oct 03 '18 at 01:13
  • According to https://stackoverflow.com/questions/42691697/using-repository-annotation-when-implementing-jparepostiory-in-spring it's only relevant to repositories not implementing the interface `Repository` (and `JpaRepository` extends that too). I will remove that from my answer. – Hero Wanders Oct 03 '18 at 01:18