The SpringBoot application fails to load with error org.springframework.beans.factory.UnsatisfiedDependencyException

I am new to Spring Boot and created a basic Restful CRUD API project: OnlineNotes using Spring Boot-JPA-MySQL(Maven-based).
I am trying to run the project in IntelliJ and facing below error:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'noteController': Unsatisfied dependency expressed through field 'noteRepository': Error creating bean with name 'noteRepository' defined in com.example.notesApp.repository.NoteRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.example.notesApp.model.Notes
2023-11-17T13:26:26.210+05:30 ERROR 25956 --- [main] o.s.boot.SpringApplication: Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'noteController': Unsatisfied dependency expressed through field 'noteRepository': Error creating bean with name 'noteRepository' defined in com.example.notesApp.repository.NoteRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.example.notesApp.model.Notes
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:767) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:747) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:145) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:492) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1416) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:597) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:325) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:323) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:950) ~[spring-context-6.0.13.jar:6.0.13]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:616) ~[spring-context-6.0.13.jar:6.0.13]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.1.5.jar:3.1.5]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:738) ~[spring-boot-3.1.5.jar:3.1.5]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:440) ~[spring-boot-3.1.5.jar:3.1.5]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) ~[spring-boot-3.1.5.jar:3.1.5]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-3.1.5.jar:3.1.5]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-3.1.5.jar:3.1.5]
    at com.example.notesApp.NotesAppApplication.main(NotesAppApplication.java:13) ~[classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'noteRepository' defined in com.example.notesApp.repository.NoteRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.example.notesApp.model.Notes
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1770) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:598) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:325) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:323) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1417) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1337) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:764) ~[spring-beans-6.0.13.jar:6.0.13]
    ... 20 common frames omitted
Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.example.notesApp.model.Notes
    at org.hibernate.metamodel.model.domain.internal.JpaMetamodelImpl.managedType(JpaMetamodelImpl.java:192) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.managedType(MappingMetamodelImpl.java:467) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.managedType(MappingMetamodelImpl.java:97) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:82) ~[spring-data-jpa-3.1.5.jar:3.1.5]
    at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:69) ~[spring-data-jpa-3.1.5.jar:3.1.5]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:246) ~[spring-data-jpa-3.1.5.jar:3.1.5]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:211) ~[spring-data-jpa-3.1.5.jar:3.1.5]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:194) ~[spring-data-jpa-3.1.5.jar:3.1.5]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:81) ~[spring-data-jpa-3.1.5.jar:3.1.5]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:317) ~[spring-data-commons-3.1.5.jar:3.1.5]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:279) ~[spring-data-commons-3.1.5.jar:3.1.5]
    at org.springframework.data.util.Lazy.getNullable(Lazy.java:245) ~[spring-data-commons-3.1.5.jar:3.1.5]
    at org.springframework.data.util.Lazy.get(Lazy.java:114) ~[spring-data-commons-3.1.5.jar:3.1.5]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:285) ~[spring-data-commons-3.1.5.jar:3.1.5]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:132) ~[spring-data-jpa-3.1.5.jar:3.1.5]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1817) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1766) ~[spring-beans-6.0.13.jar:6.0.13]
    ... 30 common frames omitted


screenshot of stacktrace

I checked other answers in stackOverFlow and implemented the solution, still facing the issue.

I have created project structure from Spring Initialzr with below structure:

    notesApp
├── src
│   ├── main
│   │   ├── java
│   │   │   ├── com
│   │   │   │   ├── example
│   │   │   │   │   ├── notesApp
│   │   │   │   │   │   ├── controller
│   │   │   │   │   │   │   ├── NoteController.java
│   │   │   │   │   │   ├── model
│   │   │   │   │   │   │   ├── Notes.java
│   │   │   │   │   │   ├── repository
│   │   │   │   │   │   │   ├── NoteRepository.java
│   │   │   │   │   │   ├── NotesAppApplication.java

Below is the list of project files created:

  1. Model class: Notes.java
@Entity//Marks class as entity class for component scan
@Table (name = "notes")//Creates table named notes in DB
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"createdAt","updatedAt"}, allowGetters = true)//Makes variables un-editable for users by ignoring values if client provides in put/update service call

public class Notes implements Serializable {

    @Id//Defines upcoming var as primary key in table
    @GeneratedValue (strategy = GenerationType.IDENTITY)//Allows auto-increment for primary key
    private long id;

    @NotBlank
    private String title;

    @NotBlank
    private String content;

    @Column(nullable = false, updatable = false)//Defines column properties in DB
    @Temporal(TemporalType.TIMESTAMP)//Converts dates in appropriate formats in DB
    @CreatedDate
    private Date createdAt;

    @Column(nullable = false, updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    @LastModifiedDate
    private Date updatedAt;
  1. Repository class: NoteRepository.java
package com.example.notesApp.repository;

import com.example.notesApp.model.Notes;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository//Asks Spring to bootstrap the repository during component scan
public interface NoteRepository extends JpaRepository<Notes, Long> {

}

3.Controller : NoteController.java

//Controller to create, retrieve, Update, delete a note
@RestController //Combination of @Controller and @ResponseBody
//@Controller defines this class is Controller to be bootstrapped while component scan
//@ResponseBody indicates the return value of method will be the response body
@RequestMapping(value = "/api")
//@RequestMapping defines the pattern of request URL
public class NoteController {


    @Autowired
    NoteRepository noteRepository;

    //Get all notes
    @GetMapping(value = "/notes")//@GetMapping handles the HTTP Get request and maps GET /notes/ URL to getAllNotes method
    public List<Notes> getAllNotes(){
        return noteRepository.findAll();
    }

    //Get a single note
    @GetMapping(value = "notes/{id}") //@GetMapping handles the HTTP Get request and maps GET /notes/{id} URL to getNotesById method
    public Notes getNotesById(@PathVariable(value = "id") long id){
        return noteRepository.findById(id).orElseThrow(()->new ResourceNotFoundException("Note","Id",id));

    }

    //Create a note
    @PostMapping (value = "/notes")//@PostMapping handles the HTTP POST request and maps POST /notes/ URL to createNote method
    public Notes createNote (@Valid @RequestBody Notes notes){
        //@Valid makes sure Request Body is valid as Title and Content of request is assigned to @NotNull annotation
        //@RequestBody binds request with method parameters
        return noteRepository.save(notes);
    }

    //Update a note
    @PutMapping(value = "notes/{id}")//@PutMapping handles the PUT request and maps PUT /notes/{Id} URl to updateNote method
    public Notes updateNote(@PathVariable(value = "id")long noteId, Notes UpdatedNote){
        Notes note = noteRepository.findById(noteId).orElseThrow(()->new ResourceNotFoundException("Note","Id",noteId));
        //Only Title and content are eligible for update
        note.setTitle(UpdatedNote.getTitle());
        note.setContent(UpdatedNote.getContent());

        return noteRepository.save(note);
    }

    //Delete a note
    @DeleteMapping(value = "notes/{id}")//@DeleteMapping handles DELETE request and maps DELETE notes/{id} to deleteNote method
    public void deleteNote(@PathVariable(value = "id")long id){
        Notes note = noteRepository.findById(id).orElseThrow(()-> new ResourceNotFoundException("Note","Id",id));
        noteRepository.delete(note);
    }
}

Please help in resolving the error.

  • can you please add the error message of the stacktrace following the “Caused by” that’s visible in your screenshot to your question?

    – 




  • @cyberbrain : Updated the complete error stack trace

    – 

  • 1

    Let me guess you are “following” a tutorial and decided to upgrade to the newest available library versions. Then you got compilation errors and decided to add a dependency to fix the compilation errors. In your entity replace the javax.persistence imports with jakarta.persistence and remove the additional dependencies you have.

    – 

  • Hello @M.Deinum, thanks a lot, it worked. Also, you guessed everything right!! 🙂

    – 




Leave a Comment