Putting annotations on methods forces JPA to access properties via methods. It makes sense when internal state of your object differs from the database schema:
@Entity
public class Employee {
private String firstName;
private String lastName;
@Column(name = "EMP_NAME") // Due to legacy database schema
public String getName() {
return fisrtName + " " + lastName;
}
public void setName(String name) {
...
}
... Getters and setters for firstName and lastName with @Transient ...
}
In JPA 2.0 you can specify access type at fine-grained level with @Access
:
@Entity @Access(AccessType.FIELD)
public class Employee {
@Access(AccessType.PROPERTY) @Column(name = "EMP_NAME")
public String getName() { ... }
... other properties have field access ...
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…