liveBook · Manning

Accessor method

As explained in Java Persistence with Spring Data and Hibernate epub

A setter method is an accessor method that assigns a value to an object property.

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

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Defines getter and setter naming, demonstrates accessor logic, and compares field access, but stops short of provider-specific configuration.
Examples
Uses User username accessors, computed and parsing name accessors, and Item validation performed through a setter-created state.
What it assumes
Assumes basic Java classes and properties while introducing JavaBeans accessor naming and persistence-provider access behavior.
What you can do after
Apply JavaBeans naming and choose between accessor and field access when designing persistence-capable classes.
How it connects
Connects accessor methods with JavaBeans, Hibernate, JPA, Hibernate Validator, Bean Validation, and field access.

Also explained in

Compare how 13 books explain accessor method →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.