当前位置: 首页 > 知识库问答 >
问题:

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

陶福
2023-03-14
        @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";
    }

}

共有1个答案

慕云
2023-03-14

尝试将专用字符串FirstName,LastName,email;更改为专用字符串FirstName,LastName,email;

应该管用。

subscriberrepository中的findByFirstName尝试按照约定查找不存在的字段FirstName

 类似资料: