An accessor method exposes an object property through a naming convention. A getter retrieves a value, while a setter assigns one. JavaBeans names getters with get plus the capitalized property name, and setters with set plus that name; Boolean getters may use is. section 3.2.3
Persistent classes commonly keep fields private or protected and provide accessors for their business attributes. Hibernate can use those methods, but it can also access fields directly; access strategy is therefore separate from whether the class exposes accessors. section 3.2.3
Basic property accessor
A User property follows the JavaBeans pattern: private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } The methods can also contain domain logic. For example, a setName method can split one input string into firstname and lastname, while getName combines those fields.
Use accessor methods with these persistence and validation considerations:
- Keep fields or methods non-public when direct state access should be restricted; configure field access when accessors are absent or deliberately excluded. section 3.2.3
- Put Bean Validation constraints on getter methods when validation should use property access; annotations on setters are not supported. section 3.3.2
- Treat validation access and JPA access independently: Hibernate Validator may call accessors while Hibernate ORM accesses fields. section 3.3.2












