@abozhov

Почему возникает ошибка Error creating bean with name 'studentRepository' defined in StudentRepository defined in @EnableJpaRepositories?

Переделываю под hibernate, до этого на jdbc работало, сейчас решил, убрать dao, и добавить это. При запуске ошибка:
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-07-20T03:58:45.861+03:00 ERROR 18240 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentsTableApplication': Unsatisfied dependency expressed through field 'studentRepository': Error creating bean with name 'studentRepository' defined in ru.bozhov.studentstable.repositories.StudentRepository defined in @EnableJpaRepositories declared on StudentsTableApplication: Not a managed type: class ru.bozhov.studentstable.models.Student
Контроллер
package ru.bozhov.studentstable.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import ru.bozhov.studentstable.models.Student;
import ru.bozhov.studentstable.services.StudentService;

import java.sql.SQLException;

@Controller
public class StudentController {
    private final StudentService service;
    @Autowired
    public StudentController(StudentService service) {
        this.service = service;
    }

    @GetMapping("/students/table")
    public String getTable(Model model) throws SQLException {
        model.addAttribute("students",service.getStudents());
        return "students";
    }
    @GetMapping("/students/new")
    public String createStudentForm(Model model) {
        Student student = new Student();
        model.addAttribute("student", student);
        return "create_student";
    }
    @PostMapping("/students/create")
    public String save(@ModelAttribute("student")  Student student) {
        service.saveStudent(student);
        return "redirect:/students/table";
    }
    @GetMapping("/students/delete/{id}")
    public String delete(@PathVariable(name = "id") int id){
        service.deleteStudent(id);
        return "redirect:/students/table";
    }
    @GetMapping("/students/edit/{id}")
    public String editForm(@PathVariable int id, Model model) {
        model.addAttribute("student", service.getStudentById(id));
        return "edit_student";
    }
    @PostMapping("/students/update/{id}")
    public String update(@PathVariable(name = "id") int id,@ModelAttribute("student")  Student student){
        service.updateStudent(student);
        return "redirect:/students/table";
    }
}

Класс
package ru.bozhov.studentstable.models;

import org.springframework.stereotype.Component;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "students")
public class Student {
    @Id
    @Column(name = "id")
    private Integer id;
    @Column(name = "name")
    private String name;
    @Column(name = "surname")
    private String surname;
    @Column(name = "age")
    private int age;
    @Column(name = "phone")
    private String phone;
    @Column(name = "email")
    private String email;
    public Student() {}
    public Student(int id, String name, String surname, int age, String phone, String email) {
        this.id = id;
        this.name = name;
        this.surname = surname;
        this.age = age;
        this.phone = phone;
        this.email = email;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

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

package ru.bozhov.studentstable.repositories;

import org.springframework.data.jpa.repository.JpaRepository;

import org.springframework.stereotype.Repository;
import ru.bozhov.studentstable.models.Student;
@Repository
public interface StudentRepository extends JpaRepository<Student, Integer> {
}

Помник
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>ru.bozhov</groupId>
    <artifactId>studentsTable</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>studentsTable</name>
    <description>studentsTable</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.6.0</version>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  • Вопрос задан
  • 482 просмотра
Решения вопроса 1
@abozhov Автор вопроса
@SpringBootApplication
@EnableJpaRepositories
@EntityScan("ru.bozhov.studentstable.models")
public class StudentsTableApplication {
    public static void main(String[] args) {
        SpringApplication.run(StudentsTableApplication.class, args);
    }

}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы