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











