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











