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 is a domain object with persistent identity and an independent lifecycle. Choose an entity when an application must load, save, or delete an object independently, or when other objects can share a reference to it. A value type instead belongs to an entity and has no persistent identifier of its own. section 5.1.2

Is an address an entity?

Two user accounts may refer to one shared Address. If deleting either account must leave that Address available to the other, model Address as an entity. If each account owns a separate Address that can disappear with the account, model it as a value type. section 5.1.2

In JPA, a persistent entity class needs both @Entity and an @Id property. The identifier exposes its database identity: two instances with the same identifier represent the same row. By default, an Item entity maps to an ITEM table, and its id property maps to the ID primary-key column. Placing @Id on a field makes field access the default for persistent state. section 2.3.2 section 5.2.2

Listing 2.3 The Message class
Path: Ch02/helloworld/src/main/java/com/manning/javapersistence/ch02
 /Message.java
 
@Entity                                                         #1
public class Message {
 
    @Id                                                         #2
    @GeneratedValue(strategy = GenerationType.IDENTITY)         #3
    private Long id;
 
    private String text;                                        #4
 
    public String getText() {                                   #4
        return text;                                            #4
    }                                                           #4
 
    public void setText(String text) {                          #4
        this.text = text;                                       #4
    }                                                           #4
}
#1 Every persistent entity class must have at least the <kbd>@Entity</kbd> annotation. Hibernate maps this class to a table called <kbd>MESSAGE</kbd>.
#2 Every persistent entity class must have an identifier attribute annotated with <kbd>@Id</kbd>. Hibernate maps this attribute to a column named <kbd>id</kbd>.
#3 Someone must generate identifier values; this annotation enables automatic generation of ids.
#4 We usually implement regular attributes of a persistent class with private fields and public getter/setter method pairs. Hibernate maps this attribute to a column called <kbd>text</kbd>.

A minimal Message entity combines an identifier, generated-value mapping, and ordinary Java property accessors. section 2.3.2

Keep the Java class usable as a domain object while accounting for persistence:

  • Provide a no-argument constructor; Hibernate uses reflection to instantiate entities. A package-visible constructor also supports runtime-generated proxies. section 3.2.3
  • Keep persistent classes and methods non-final for JPA portability and proxy-based performance. Put state in attributes and domain behavior in methods. section 3.2.3
  • Use annotations beside the class or external XML mapping metadata. Annotations are convenient for refactoring; XML keeps mapping outside the source. section 3.3.1 section 3.0

Entity state also depends on its persistence context. Hibernate detects changes to a managed entity during a transaction and updates the database without an explicit update call. To save changes from a detached entity, merge it and continue using the returned managed instance, not the stale detached reference. section 2.3.3 section 10.3.4

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Works from entity-versus-value decisions through Java mapping and identifier access to managed and detached instance behavior.
Examples
Uses a Message entity, an Item mapping, and shared versus independently owned addresses.
What it assumes
Assumes Java classes and database tables; introduces persistent identity and mapping with a small Message class.
What you can do after
Build it from scratch as a JPA-mapped Java class and choose entity identity over value ownership when lifecycle requires it.
How it connects
Connects entities to value types, domain models, object-relational mapping, primary keys, and persistence contexts.

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.