小编典典

Spring Data JPA无法找到具有给定名称的Attribute

spring-boot

我试图用Spring Data JPASpring Boot和我一直得到错误,我想不通的问题是什么:

在此ManagedType
[com.example.h2demo.domain.Subscriber]上找不到具有给定名称[firstName]的Attribute

FirstName在我的实体类中声明。之前我在其他项目中使用过DAO的服务类,并且运行良好。

我的Entity类(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;
          }
        }
...

我的存储库类

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

}

阅读 350

收藏
2020-05-30

共1个答案

小编典典

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

它应该工作。

findByFirstNameSubscriberRepository试图找到一个场firstName按照惯例是不存在的。

2020-05-30