无法找到具有给定名称的属性

无法找到具有给定名称的属性

本文介绍了Spring Data JPA 无法找到具有给定名称的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 Spring Boot 上使用 Spring Data JPA 并且我不断收到错误,我无法弄清楚问题是什么:

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:

无法找到具有给定名称 [firstName] 的属性这个 ManagedType [com.example.h2demo.domain.Subscriber]

FirstName 在我的实体类中声明.我之前在不同的项目中使用过一个带有 DAO 的服务类并且工作得很好.

FirstName is declared in my entity class. I have used a service class with DAO before with different project and worked perfectly.

我的实体类(getter 和 setter 也在类中):

My Entity class (getters and setters are also in the class) :

        @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;
          }
        }
...

我的存储库类

@Component
public interface SubscriberRepository extends JpaRepository<Subscriber,Long> {
    Subscriber findByFirstName(String FirstName);
    Subscriber deleteAllByFirstName(String FirstName);
}

我的服务类

@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);
    }
}

和我的控制器类:

@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";
    }

}

推荐答案

尝试将 private String FirstName,LastName,Email; 更改为 private String firstName,lastName,email;

它应该可以工作.

findByFirstName 尝试按照约定查找不存在的字段 firstName.

findByFirstName in SubscriberRepository tries to find a field firstName by convention which is not there.

关于如何遍历实体内部的属性的进一步参考 https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-property-expressions

Further reference on how properties inside the entities are traversed https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-property-expressions

这篇关于Spring Data JPA 无法找到具有给定名称的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 10:25