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
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
