gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:12:06 27 4
gpt4 key购买 nike

出于某种原因,我在尝试使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com