1

New to java and spring. Tried creating an app but was unsuccessful.I have the following entity and controller in my app. But at runtime i get an error. I posted snippets for easier reading.

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);

And the error that is thrown at runtime is

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

Please advice on what to do.

B.obed
  • 107
  • 9
  • possible dublicate: https://stackoverflow.com/questions/4381290/hibernate-exception-org-hibernate-annotationexception-no-identifier-specified – Mustafa Çil Dec 02 '19 at 19:01
  • Does this answer your question? [hibernate exception: org.hibernate.AnnotationException: No identifier specified for entity: com..domain.idea.MAE\_MFEView](https://stackoverflow.com/questions/4381290/hibernate-exception-org-hibernate-annotationexception-no-identifier-specified) – diogenesgg Dec 02 '19 at 20:05

1 Answers1

1

Your Staff entity lacks a member with an @Id annotation. This could be added to staff_id like follows:

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

    ....
}
ldz
  • 2,217
  • 16
  • 21
  • how do i do this? – B.obed Dec 02 '19 at 19:20
  • great but that led to this error. ```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)] ``` – B.obed Dec 02 '19 at 19:46
  • Which means you're one step further. Feel free to open a new question or search for how to properly map `@OneToMany` relationships. – ldz Dec 02 '19 at 19:49