java - Spring 运行时错误,没有为实体指定标识符:
问题描述
java和spring的新手。尝试创建应用程序但未成功。我的应用程序中有以下实体和控制器。但在运行时我得到一个错误。我发布了片段以便于阅读。
staff.java
@Data
@Entity
public class Staff {
private int staff_id;
private String staff_name;
private String staff_email;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "staff")
private List<VisitRequest> visitRequest;
public String getStaff_name(){
return staff_name;
}
public void setStaff_name(String staff_name){
this.staff_name = staff_name;
}
public String getStaff_email(){
return staff_email;
}
public void setStaff_email(String staff_email){
this.staff_email = staff_email;
}
public int getStaff_id(){
return staff_id;
}
public void setStaff_id(int staff_id){
this.staff_id = staff_id;
}
}
StaffController.java
@Controller
@RestController
@RequestMapping("/staff/")
public class StaffController{
@Autowired
protected StaffRepository staffRepository;
@GetMapping("/Staff")
public List<Staff> getAllStaff() {
return staffRepository.findAll();
}
@GetMapping("/staff/{Staff_id}")
public ResponseEntity<Staff> getStaffById(@PathVariable(value = "Staff_id") Long Staff_id)
throws ResourceNotFoundException{
Staff staff = staffRepository.findById(Staff_id)
.orElseThrow(() -> new ResourceNotFoundException("Employee not Found"));
return ResponseEntity.ok().body(staff);
运行时抛出的错误是
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfigura
tion.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.Vportal.data.model.Staff
请就该怎么做提出建议。
解决方案
您的Staff
实体缺少带@Id
注释的成员。这可以添加staff_id
如下:
@Data
@Entity
public class Staff {
@Id
private int staff_id;
....
}
推荐阅读
- php - Wordpress - 自定义字段的自定义查询
- java - spring项目中的多线程无法正常工作
- c# - 如何修复我尝试了各种解决方案的 Appium 和 C# 中的“NoElementException”?
- woocommerce - 您需要 Woocommerce 的儿童主题吗?
- assembly - AMD64 调用约定;__m512 操作数在栈上
- android - 无法从病房的 Android 8.1 版本中捕获 Android TV Box 中的音量键事件
- bison - 如何从特定节点开始打印 YACC 语法图?
- facebook - 为什么 Facebook Leadgen 广告测试工具会出现 102 服务器错误?
- asp.net - IApplication Builder 不包含 UseWebpackDevMiddleware 的定义
- vb.net - VB.net // 你如何将一个数字向下取整?