我试图用Spring Data JPA在Spring Boot和我一直得到错误,我想不通的问题是什么:
Spring Data JPA
Spring 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"; } }
尝试更改private String FirstName,LastName,Email;为private String firstName,lastName,email;
private String FirstName,LastName,Email;
private String firstName,lastName,email;
它应该工作。
findByFirstName在SubscriberRepository试图找到一个场firstName按照惯例是不存在的。
findByFirstName
SubscriberRepository
firstName