【问题标题】:Spring Runtime Error,No identifier specified for entity:Spring 运行时错误,没有为实体指定标识符:
【发布时间】:2020-03-27 10:04:55
【问题描述】:

Java 和 Spring 的新手。尝试创建应用程序但未成功。我的应用程序中有以下实体和控制器。但在运行时我得到一个错误。我发布了 sn-ps 以便于阅读。

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

请指教该怎么做。

【问题讨论】:

标签: java spring hibernate


【解决方案1】:

您的Staff 实体缺少带有@Id 注释的成员。这可以添加到staff_id,如下所示:

@Data
@Entity
public class Staff {
    @Id
    private int staff_id;

    ....
}

【讨论】:

  • 我该怎么做?
  • 很好,但这导致了这个错误。 nested exception is org.hibernate.MappingException: Could not determine type for: java.util.List, at table: staff, for columns: [org.hibernate.mapping.Column(visit_request)]
  • 这意味着你更进一步。随意打开一个新问题或搜索如何正确映射@OneToMany 关系。
猜你喜欢
  • 2014-04-06
  • 1970-01-01
  • 2015-07-25
  • 2016-01-31
  • 2018-08-13
  • 1970-01-01
  • 2018-10-28
  • 1970-01-01
相关资源
最近更新 更多