Michał Bil
Michał Bil

Reputation: 3599

Spring - can't create custom query in CrudRepository

For some reason i get BeanCreationException when trying to initialize repository with findByEmail method. Don't know what's the cause, field in entity corresponds with arguments in query and packages are scanned in dispatcher 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);

    }

}

Full trace of error :

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

Upvotes: 0

Views: 812

Answers (1)

Robert Moskal
Robert Moskal

Reputation: 22553

I think you left the query annotation off of your repository interface method::

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

Upvotes: 1

Related Questions