Ярига Олег
Ярига Олег

Reputation: 341

How to correctly provide CRUD repository to be able to view and create data in DB (I'm getting error with beans)?

I started studying programming with Java and face the problem I can't resolve for couple of days. I use JDK-13 and Spring Boot 2.4.4, and when I try to add CRUD Repository in my project and launch the server, I get an error:

Description:

Field noteRepo in com.javatechnologies.zettelkasten.MainController required a bean of type 'com.javatechnologies.zettelkasten.NoteRepository' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.javatechnologies.zettelkasten.NoteRepository' in your configuration.

Additional info and link to a github repository provided below. If anyone can help me, I will be very grateful

I have a db model:


@Entity
public class Note {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

    private String title;
    private String text;
    private String tag;

    // empty constructor for spring model generation
    public Note(){
    }

    public Note(String title, String text, String tag){
        this.title = title;
        this.text = text;
        this.tag = tag;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public Integer getId() {
        return id;
    }

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

and repository for this one:

package com.javatechnologies.zettelkasten;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Component;

@Component
public interface NoteRepository extends CrudRepository<Note, Long> {}

And now I try to use this repository in my Controller:

@Controller
public class MainController {
    @Autowired
    private NoteRepository noteRepo;

    @GetMapping("/")
    public String mainPaige(Map<String, Object> model) {
        Iterable<Note> notes = noteRepo.findAll();

        model.put("notes", notes);
        return "mainPage";
    }
...

If you need additional information, you can got to the repository on github: https://github.com/OlegYariga/zettelkasten

Upvotes: 0

Views: 103

Answers (1)

Jens
Jens

Reputation: 69470

If you are tring touse JAP, you miss the spring-boot-starter-data-jpa dependency in your pom:

   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

And you have to add @EnableJpaRepositories to your main Class:

@SpringBootApplication
@EnableJpaRepositories
public class ZettelkastenApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZettelkastenApplication.class, args);
    }
}

And annotate the repository with @Repository

Upvotes: 2

Related Questions