java - Spring - 无法在 CrudRepository 中创建自定义查询

标签 java spring spring-mvc web-applications

出于某种原因,我在尝试使用 findByEmail 方法初始化存储库时收到 BeanCreationException 。不知道是什么原因,实体中的字段与查询中的参数相对应,并且在调度程序 servlet 中扫描包。

PersonRepository.java

import java.util.List;

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

import wymysl.model.Person;

@Repository
public interface PersonRepository extends CrudRepository<Person, Long> {

    public List<Person> findByEmail(String Email);

}

Person.java

@Entity
@Table(name = "Persons")
public class Person {

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    protected Integer id_per;

    @NotNull
    @Size(min=2, max=30)
    protected String Name;

    @NotNull
    @Size(min=2, max=30)
    protected String Surname;

    @NotNull
    @Email
    protected String Email;

    protected String Contact_email;

    protected String Gender;

    protected String City;

    protected String Country;

    protected String Additional;

    @NotNull
    @Size(min=6, max=15)
    protected String Password;

    @Transient 
    @NotNull
    @Size(min=6, max=15)
    protected String checkPassword;


    public Person(){

    }

    public Person(String name, String surname, String email, String password) {
        this.Name = name;
        this.Surname = surname;
        this.Email = email;
        this.Password = password;
    }


    public Integer getId_per() {
        return id_per;
    }
    public void setId_per(Integer id_per) {
        this.id_per = id_per;
    }
    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 String getEmail() {
        return Email;
    }
    public void setEmail(String email) {
        this.Email = email;
    }
    public String getContact_email() {
        return Contact_email;
    }
    public void setContact_email(String contact_email) {
        this.Contact_email = contact_email;
    }
    public String getGender() {
        return Gender;
    }
    public void setGender(String gender) {
        this.Gender = gender;
    }
    public String getCity() {
        return City;
    }
    public void setCity(String city) {
        this.City = city;
    }
    public String getCountry() {
        return Country;
    }
    public void setCountry(String country) {
        this.Country = country;
    }
    public String getAdditional() {
        return Additional;
    }
    public void setAdditional(String additional) {
        this.Additional = additional;
    }
    public String getPassword() {
        return Password;
    }
    public void setPassword(String password) {
        this.Password = password;
    }

    public String getCheckPassword() {
        return checkPassword;
    }

    public void setCheckPassword(String checkPassword) {
        this.checkPassword = checkPassword;
    }

}

PersonValidator.java

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import wymysl.model.Person;
import wymysl.repositories.PersonRepository;

public class PersonValidator implements Validator {

    @Autowired
    PersonRepository repo;

    public boolean supports(Class<?> classz) {
        // TODO Auto-generated method stub
        return Person.class.equals(classz);
    }

    public void validate(Object target, Errors errors) {

        Person person = (Person) target;

        if(!(person.getPassword().equals(person.getCheckPassword()))){
            errors.rejectValue("Password", "password.nonequal");
        }

        List<Person> emailCheck = repo.findByEmail(person.getEmail());

        if(!emailCheck.isEmpty()) {
            errors.rejectValue("Email", "email.exist");
        }

        repo.save(person);

    }

}

错误的完整跟踪:

Could not create query metamodel for method public abstract java.util.List wymysl.repositories.PersonRepository.findByEmail(java.lang.String)!

最佳答案

我认为您将查询注释从存储库接口(interface)方法中删除了::

@Query
public List<Person> findByEmail(String Email);

关于java - Spring - 无法在 CrudRepository 中创建自定义查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32225423/

相关文章:

java - 仅具有 gettable 的 JSF

java - 加载相关实体时实体图被忽略

java - 哪个方面/拦截器处理@Transactional注释

java - 在 Spring Boot 中排除 BootstrapConfiguration 类进行测试

spring - 获取 Spring 3.1 MVC View 的渲染输出

java - Spring MVC request mapping not found 404页面未找到

spring-mvc - Spring Security 4.2中的StrictHttpFirewall与Spring MVC @MatrixVariable

java - JSF2 ApplicationScope bean实例化时间?

java - 具有返回对象数组的特定列的 JPQL

java - 连接错误 : TypeError ('ref() does not take keyword arguments' , )})