What is the use of generating getters and setters in a class annotated with @Entity (Hibernate)?
When do they get invoked?
@Entity
@Table(name="student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private int id;
private String firstName;
private String lastName;
public Student() {
}
public Student(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
//other getters and setters
}
>Solution :
As soon as your fields are private then you need getters and setters to access them outside of the entity class.