@Espio

Как исправить проблему с интерфейсом CrudRepository?

Делал web приложение по гайду, но у меня почему-то не работает функция FindBy. Прошу о помощи.

Класс сущностей
package com.examle.TableGames.domain;


import javax.persistence.*;

@Entity
public class Users {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer idUser;

    @Column(name = "NameUser")
    private String NameUser;
    @Column(name = "Phone")
    private String Phone;
    @Column(name = "Email")
    private String Email;

    public Users() {
    }

    public Users(String nameUser, String phone, String email) {
        this.NameUser = nameUser;
        this.Phone = phone;
        this.Email = email;
    }



    public Integer getId() {
        return idUser;
    }

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

    public String getNameUser() {
        if (NameUser == null){
            NameUser="0";}
        return NameUser;
    }

    public void setNameUser(String nameUser) {
        NameUser = nameUser;
    }

    public String getPhone() {
        if (Phone==null){Phone=" ";}
        return Phone;
    }

    public void setPhone(String phone) {
        Phone = phone;
    }

    public String getEmail() {
        return Email;
    }

    public void setEmail(String email) {
        Email = email;
    }
}


Репозиторий
package com.examle.TableGames.repos;

import com.examle.TableGames.domain.Users;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UsersRepo extends CrudRepository<Users, Integer> {

    List<Users> findByNameUser(String nameUser);

}


package com.examle.TableGames;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}


Контроллер
package com.examle.TableGames;

import com.examle.TableGames.domain.Users;
import com.examle.TableGames.repos.UsersRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Controller
public class GreetingController {

    @Autowired
    private UsersRepo usersRepo;

    @GetMapping("/greeting")
    public String greeting
            (@RequestParam(name="name", required=false, defaultValue="World") String name, Map<String, Object> model)
    {

        model.put("name", name);
        return "greeting";
    }

    @GetMapping
    public String main (Map<String, Object> model){
        Iterable<Users> users = usersRepo.findAll();

        model.put("users", users);
        return "main";
    }

    @PostMapping
    public String add(@RequestParam String NameUser, @RequestParam String Phone, @RequestParam String Email, Map<String, Object> model){

        Users user = new Users(NameUser, Phone, Email);

        usersRepo.save(user);

        Iterable<Users> users = usersRepo.findAll();

        model.put("users", users);

        return "main";
    }

    @PostMapping("filter")
    public String filter(@RequestParam String filter, Map<String, Object> model){

        Iterable<Users> users;

        if (filter != null && !filter.isEmpty()) {
            users = usersRepo.findByNameUser(filter);
        } else {
            users = usersRepo.findAll();
        }

        model.put("users", users);

        return "main";
    }

}


<html>

<body>
    <div>
        <form method="post">
            <input type="text" name="NameUser" placeholder="Введите имя">
            <input type="number" name="Phone" placeholder="Телефон">
            <input type="email" name="Email" placeholder="Email">
            <button type="submit">Добавить</button>
        </form>
    </div>
    <div>Список пользователей</div>
    <form method="post" action="filter">
        <input type="text" name="filter">
        <button type="submit">Найти</button>
    </form>
    {{#users}}
        <div>
        <b>{{id}}</b>
        <span>{{nameUser}}</span>
        <span>{{phone}}</span>
        <span>{{email}}</span>
        </div>
    {{/users}}
</body>

</html>


LOG весь не влез, но я так понимаю это основная ошибка.
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'greetingController': Unsatisfied dependency expressed through field 'usersRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usersRepo': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.examle.TableGames.repos.UsersRepo.findByNameUser(java.lang.String)! Unable to locate Attribute  with the the given name [nameUser] on this ManagedType [com.examle.TableGames.domain.Users]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at com.examle.TableGames.Application.main(Application.java:11) ~[classes/:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
	at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.2.2.RELEASE.jar:2.2.2.RELEASE]
  • Вопрос задан
  • 339 просмотров
Решения вопроса 1
@koperagen
В исключении написано Unable to locate Attribute with the the given name [nameUser]
Т.е. соглашение именовать поля с маленькой буквы, а у вас с большой. Попробуйте назвать поле nameUser
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

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

Похожие вопросы