liveBook · Manning

Object persistence

As explained in Java Persistence with Spring Data and Hibernate epub

Object persistence is the preservation and later reconstruction of individual objects across application-process lifetimes.

Object persistence lets individual objects outlive an application process: their data is saved to a store and the objects can be recreated later. In Java, this often means mapping object instances to a database using SQL. section 1.1

A relational database cannot store a network of objects directly in a table. The network must be disassembled into columns of SQL data types and reconstructed as objects. Object/relational mapping (ORM) reduces this work, including writing joins and copying JDBC result values into objects, but it is not a solution for every persistence task. section 1.4

Persisting associations

A User can refer to a set of BillingDetails objects, while each BillingDetails object refers back to a User. These are two directional Java references; a relational association instead uses key values and a foreign key constraint. A many-to-many relationship requires a link table, even though that table may have no corresponding domain-model class. section 1.2.4

Path: Ch01/e-commerce/src/com/manning/javapersistence/ch01/User.java
 
public class User {
    private Set<BillingDetails> billingDetails = new HashSet<>();
}
 
 
Path: Ch01/e-commerce/src/com/manning/javapersistence/ch01
 /BillingDetails.java
 
public class BillingDetails {
    private User user;
}

The Java classes show the two directions of a one-to-many association. section 1.2.4

CREATE TABLE USER_BILLINGDETAILS (
    USER_ID BIGINT,
    BILLINGDETAILS_ID BIGINT,
    PRIMARY KEY (USER_ID, BILLINGDETAILS_ID),
    FOREIGN KEY (USER_ID) REFERENCES USERS(ID),
    FOREIGN KEY (BILLINGDETAILS_ID) REFERENCES BILLINGDETAILS(ID)
);

The SQL link table represents a many-to-many association with two foreign keys. section 1.2.4

Full ORM is most suitable for applications with a rich domain model. A table-oriented design may be sufficient for simpler applications, and learning ORM may not justify its overhead for a trivial use case. Not every domain class needs persistent instances: some exist only at runtime. section 3.1.3

In Java, Jakarta Persistence API (JPA) is a persistence specification, and Hibernate is a JPA implementation. Spring Data can sit above JPA implementations to simplify persistence further; it also supports access to other kinds of stores, including NoSQL databases. section 1.4 section 1.0

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains object reconstruction and relational association mapping, including foreign keys and many-to-many link tables.
Examples
Uses User and BillingDetails classes and a SQL link table to illustrate one-to-many and many-to-many associations.
What you can do after
Evaluate when full ORM suits a rich domain model and when a table-oriented approach may suffice.
How it connects
Connects persistence to object/relational mapping, domain models, SQL, JDBC, and association mapping.

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.