Home > front end >  Error creating bean with name: Unsatisfied dependency expressed through field
Error creating bean with name: Unsatisfied dependency expressed through field

Time:01-09

I cant figure it out whats the problem, to me it seems that i have already taken all the necessary steps, simple testing program and it wont work, to me without any reason.


@RestController
public class StudentController {

    @Autowired
    StudentServiceInterface studentService;

    @PostMapping("/student")
    public Student save(@RequestBody Student student){
        return studentService.saveStudent(student);
    }
}

public interface StudentServiceInterface {
    Student saveStudent(Student student);
}

@Service
public class StudentServiceImpl implements StudentServiceInterface{

    @Autowired
    StudentRepositoryInterface studentRepository;

    @Override
    public Student saveStudent(Student student) {
        return studentRepository.save(student);
    }
}

@Entity
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long Id;
    private String name;
    private String surname;
    private Integer age;
    ...
}

folder structure

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentController': Unsatisfied dependency expressed through field 'studentService': Error creating bean with name 'studentServiceImpl': Unsatisfied dependency expressed through field 'studentRepository': Error creating bean with name 'studentRepositoryInterface' defined in com.example.springstranica.repository.StudentRepositoryInterface defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.example.springstranica.entity.Student

I have looked some of the answers on stack and no solution for me. Pardon me if i skiped something. Can someone help and find where i did wrong and how can i prevent this from happening again.

The full code is available at github.com.

CodePudding user response:

Spring-boot switched from JavaEE Persistence to JakartaEE Persistence from Release 3.0 onwards (github.com).

When we look at the pom.xml, we see:

        ...
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>
        ...

This imports the "old" JavaEE API, which is - as stated above - no longer supported by spring-boot and thus useless for us (but this is not directly the source of the exception).

Looking at Student's imports, we see:

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

So we use the "old" API in class Student, which is why spring reports that this entity is not managed (since the correct @Entity annotation is missing).

The solution is straightforward. We:

  • remove javax.persistence-api from pom.xml (to prevent importing the wrong persistence api in the future), and
  • replace javax.persistence with jakarta.persistence in Student.

This also makes the @EntityScan in class SpringstranicaApplication superfluous.

I have opened a PR (github.com) to fix the JPA setup.

  • Related