org.hibernate.PropertyNotFoundException: Could not locate setter method for property
Today, with my colleagues, I talked about Oriented Programming and why we shouldn’t always generate all the getters and setters when we design a class (Why getter and setter methods are evil?). And someone told me that JPA forces us to declare setters and getters in an entity class. Does it?
When an entity class doesn’t contain a setter for a field, JPA will raise the following error :
org.hibernate.PropertyNotFoundException: Could not locate setter method for property
When an entity class doesn’t contain a constructor without parameters, JPA will raise the following error :
org.hibernate.InstantiationException: No default constructor for entity:
JPA needs a constructor and setter for each declared field. Does it mean we have to expose the default constructor and each setter ?
It doesn’t…
JPA supports private constructor and private setters.
@Entity
public class Person {
private long id;
private String name;
public Person(long id, String name) {
this.id = id;
this.name = name;
}
private Person() {
}
@Id
public long getId() {
return id;
}
private void setId(long id) {
this.id = id;
}
@Column
public String getName() {
return name;
}
private void setName(String name) {
this.name = name;
}
}
This entity is a valid JPA entity and it can’t be modified after its creation.
You can use the following github project to try by ourself. JpaPrivate @ GitHub