liveBook · Manning

Entity

As explained in Java Persistence with Spring Data and Hibernate epub

A business entity is an object representing a significant concept or state in an application's business domain.

An entity represents a business concept whose individual instances matter. In a persistence model, an entity has an identity that lets an application retrieve, change, or delete it independently. A User account is an entity because each account must remain distinguishable even if its other attributes change. section 5.1.2

Identity separates an entity from a value type. An Address shared by two User accounts needs its own lifecycle: deleting one account must not delete an address still referenced by the other. If each address belongs exclusively to one account, it can instead be a value type whose lifespan follows its owner. Model the intended sharing and deletion behavior before choosing the mapping. section 5.1.2

Figure 5.2 Two User instances have a reference to a single Address.

The shared Address illustrates why a separately identifiable entity may outlive one of its referring User accounts. section 5.1.2

A minimal JPA entity

@Entity public class Message { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String text; public String getText() { return text; } public void setText(String text) { this.text = text; } } The identifier exposes the database identity: two Message instances with the same identifier represent the same row. The class needs no persistence-specific superclass or interface and can also be used as an ordinary Java object. section 2.3.2 section 5.2.2

A persistent entity class uses @Entity and an @Id property. By default, the class maps to a table; placing @Id on a field selects field access, so the provider treats fields as persistent state. A no-argument constructor lets Hibernate instantiate the class. Following JPA class requirements also helps preserve portability and permits Hibernate's proxy-based optimization. section 5.2.2 section 3.2.3

Mapping makes entity instances manageable by JPA and Hibernate without putting transaction code into their business methods. Inside a transaction, changes to a managed instance can be detected and written automatically. A detached instance is different: merge() copies its state to a managed instance and returns that current instance. Continue work with the returned reference rather than the stale detached one. section 2.3.3 section 10.3.4

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Defines identity and lifecycle, then explains basic JPA mapping, field access, and detached-instance merging.
Examples
Uses a mapped Message class and User accounts whose Address is either shared or owned.
What it assumes
Assumes Java classes and properties while introducing persistent identity and the entity–value-type distinction.
What you can do after
Build it from scratch by identifying independent domain objects and mapping Java classes with @Entity and @Id.
How it connects
Contrasts entities with value types and ties them to domain models, primary keys, persistence contexts, and object-relational mapping.

Also explained in

Compare how 12 books explain entity →

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.