gpt4 book ai didi

java - Spring运行时错误,没有为实体指定标识符:

转载 作者:行者123 更新时间:2023-11-30 05:23:23 24 4
gpt4 key购买 nike

java 和 spring 新手。尝试创建应用程序但不成功。我的应用程序中有以下实体和 Controller 。但在运行时我收到错误。我发布了一些片段以便于阅读。

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;

....
}

关于java - Spring运行时错误,没有为实体指定标识符:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59144919/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com