liveBook · Manning

Plain old java object

As explained in Java Persistence with Spring Data and Hibernate epub

A POJO is an ordinary Java object that does not depend on framework-specific base classes, interfaces, or runtime mechanisms for its basic behavior.

A plain old Java object (POJO) is an ordinary Java object whose basic behavior is not bound to a framework. In a domain model, it can hold state, define business methods, and refer to other objects without embedding persistence operations in its implementation. Keeping persistence concerns separate makes business objects easier to execute and test independently. section 3.2.3 section 3.4 section 5.1.1

POJOs support fine-grained models with more classes than database tables. For example, a User can have an Address object even when the USERS table stores the address in HOME_STREET, HOME_CITY, and HOME_ZIPCODE columns. The separate class gives the address a cohesive, reusable representation. section 5.1.1

Listing 3.2 POJO implementation of the User class with logic in accessor methods
Path: Ch03/domainmodel/src/main/java/com/manning/javapersistence/ch03/ex02
 /User.java
 
public class User {
 
    private String firstname;
    private String lastname;
 
    public String getName() {
        return firstname + ' ' + lastname;
    }
 
    public void setName(String name) {
        StringTokenizer tokenizer = new StringTokenizer(name);
        firstname = tokenizer.nextToken();
        lastname = tokenizer.nextToken();
    }
 
}

This User class puts behavior in accessors: getName combines two fields, while setName splits a supplied name. section 3.2.3

Persistence can be largely transparent without being completely unrestricted. A JPA entity needs a database identifier, and collection-valued attributes must use interface types such as List or Set rather than implementation types such as HashSet. A persistent class may still depend on JPA at compile time. section 3.2.2 section 3.4

For a persistence-capable class, account for these practical constraints:

  • Use a top-level class and avoid final classes and methods for JPA portability. Hibernate permits final classes, but finality prevents proxy-based performance improvements. section 3.2.3
  • Provide a no-argument constructor, or rely on Java’s default constructor when no constructor is declared. Hibernate uses reflection to instantiate the class; the constructor must be at least package-visible for runtime-generated proxies. section 3.2.3
  • Use fields and accessors to express the domain model. For an immutable entity, values can be set in a constructor without exposing public setters; Hibernate can load and store fields directly when the identifier annotation is on a field. section 3.2.3 section 5.3.3

POJOs can also be components of a Spring application. Spring’s container manages the creation, dependency injection, and lifecycle of its managed objects, called beans. section 14.1

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains entity design constraints, constructor and proxy behavior, field access, and immutable mappings.
Examples
Illustrates accessor logic with User and fine-grained modeling with Address, then maps an immutable Bid entity.
What you can do after
Evaluate and design persistence-capable domain classes while accounting for portability, proxies, and field access.
How it connects
Connects POJOs to transparent persistence, domain models, JavaBeans, proxies, and dependency injection.

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.