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

时间:2015-08-26 11:29:04

标签: java spring spring-mvc web-applications

由于某种原因,我在尝试使用BeanCreationException方法初始化存储库时得到findByEmail。不知道原因是什么,实体中的字段与查询中的参数对应,并且在调度程序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)!

1 个答案:

答案 0 :(得分:1)

我认为您从存储库接口方法::

中删除了查询注释
@Query
public List<Person> findByEmail(String Email);