Java Spring Data JPA 无法找到具有给定名称的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49319468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Data JPA Unable to locate Attribute with the the given name
提问by smned
I was trying to use Spring Data JPA
on Spring Boot
and I kept getting error, I can't figure out what the problem is:
我试图用Spring Data JPA
在Spring Boot
和我一直得到错误,我想不通的问题是什么:
Unable to locate Attribute with the the given name [firstName] on this ManagedType [com.example.h2demo.domain.Subscriber]
无法在此 ManagedType [com.example.h2demo.domain.Subscriber] 上找到具有给定名称 [firstName] 的属性
FirstName is declared in my entity class. I have used a service class with DAO before with different project and worked perfectly.
FirstName 在我的实体类中声明。我之前在不同的项目中使用过一个带有 DAO 的服务类并且工作得很好。
My Entity class (getters and setters are also in the class) :
我的实体类(getter 和 setter 也在类中):
@Entity
public class Subscriber {
@Id @GeneratedValue
private long id;
private String FirstName,LastName,Email;
public Subscriber(long id, String firstName, String lastName, String email) {
this.id = id;
this.FirstName = firstName;
this.LastName = lastName;
this.Email = email;
}
}
...
My Repository Class
我的存储库类
@Component
public interface SubscriberRepository extends JpaRepository<Subscriber,Long> {
Subscriber findByFirstName(String FirstName);
Subscriber deleteAllByFirstName(String FirstName);
}
My Service Class
我的服务类
@Service
public class SubscriberService {
@Autowired
private SubscriberRepository subscriberRepository;
public Subscriber findByFirstName(String name){
return subscriberRepository.findByFirstName(name);
}
public Subscriber deleteAllByFirstName(String name){
return subscriberRepository.deleteAllByFirstName(name);
}
public void addSubscriber(Subscriber student) {
subscriberRepository.save(student);
}
}
And My Controller class:
和我的控制器类:
@RestController
@RequestMapping("/subscribers")
public class SubscriberController {
@Autowired
private SubscriberService subscriberService;
@GetMapping(value = "/{name}")
public Subscriber findByFirstName(@PathVariable("name") String fname){
return subscriberService.findByFirstName(fname);
}
@PostMapping( value = "/add")
public String insertStudent(@RequestBody final Subscriber subscriber){
subscriberService.addSubscriber(subscriber);
return "Done";
}
}
采纳答案by harsh
Try changing private String FirstName,LastName,Email;
to private String firstName,lastName,email;
尝试更改private String FirstName,LastName,Email;
为private String firstName,lastName,email;
It should work.
它应该工作。
findByFirstName
in SubscriberRepository
tries to find a field firstName
by convention which is not there.
findByFirstName
inSubscriberRepository
试图按照firstName
惯例找到一个不存在的字段。
回答by JPS
As per specification, the property names should start with small case.
根据规范,属性名称应以小写开头。
...The resolution algorithm starts with interpreting the entire part (AddressZipCode) as the property and checks the domain class for a property with that name (uncapitalized)....
...解析算法首先将整个部分 (AddressZipCode) 解释为属性并检查具有该名称的属性的域类(未大写)....
It will try to find a property with uncapitalized name. So use firstName
instead of FristName
and etc..
它将尝试查找名称未大写的属性。所以使用firstName
代替FristName
等。
回答by Osama Al-Banna
After I change my entity class
variables from capital letter to small letter for instance Username
to username
the method Users findByUsername(String username);
is working for me now .
我改变我的后entity class
从大写字母变量,比如小信Username
到username
的方法Users findByUsername(String username);
,现在为我工作。
回答by S.Dayneko
The same problem was when i had deal with Spring Data Specifications (https://www.baeldung.com/rest-api-search-language-spring-data-specifications)
同样的问题是当我处理 Spring Data Specifications ( https://www.baeldung.com/rest-api-search-language-spring-data-specifications)
Initial piece of code was:
最初的一段代码是:
private Specification<Project> checkCriteriaByProjectNumberLike(projectNumber: String) {
(root, query, criteriaBuilder) -> criteriaBuilder.like(root.get("project_number"), "%" + projectNumber)
}
The problem was in root.get("project_number"). Inside the method, I had to put the field name as in the model (projectNumber), but I sent the field name as in the database (project_number).
问题出在 root.get("project_number") 中。在方法内部,我必须将字段名称放在模型中 (projectNumber),但我发送的字段名称和数据库中一样 (project_number)。
That is, the final correct decision was:
也就是说,最终正确的决定是:
private Specification<Project> checkCriteriaByProjectNumberLike(projectNumber: String) {
(root, query, criteriaBuilder) -> criteriaBuilder.like(root.get("projectNumber"), "%" + projectNumber)
}