liveBook · Manning

@Entity

As explained in Java Persistence with Spring Data and Hibernate epub

The JPA annotation that declares a Java class as a persistent entity type.

Use `@Entity` to declare that a Java class represents a persistent entity type. Choose an entity for a domain object that needs its own identity, such as an item or user. A dependent value such as an address may instead be modeled as an embeddable type. section 3.3.1 section 5.1.3 section 19.2.2

A minimal entity

`@Entity public class Item { @Id private Long id; private String name; }` `@Entity` makes `Item` persistence-capable, but it is not a complete mapping without `@Id`. By default, the class maps to a table named `ITEM`, and `id` maps to its `ID` primary-key column. Because `@Id` is on a field, the provider accesses fields directly and treats the other fields as persistent state by default. section 5.2.2 section 3.3.1

Keep the class declaration distinct from the rest of its mapping:

  • Use `@Id` to expose database identity; an entity needs an identifier property. section 5.2.2
  • Map relationships and dependent values separately. An entity can, for example, contain an embedded address and a collection of related entities. section 19.2.2
  • Keep standard annotations beside the Java declarations they describe. Hibernate reads that metadata at startup; provider-specific annotations are available when standard JPA metadata is insufficient. section 3.3.1
Listing 19.4 The User class
Path: Ch19/querydsl/src/main/java/com/manning/javapersistence/querydsl
 /model/User.java
 
@Entity
@NoArgsConstructor
public class User {
 
    @Id                                                             #1
    @GeneratedValue(generator = Constants.ID_GENERATOR)             #1
    @Getter
    private Long id;
 
 
    @Embedded                                                       #2
    @Getter
    @Setter
    private Address address;                                        #2
 
    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)        #3
    private Set<Bid> bids = new HashSet<>();                        #3
 
    // . . .
}
#1 The ID field is an identifier generated by the <kbd>Constants.ID_GENERATOR</kbd> generator. For a review of generators, revisit chapter 5.
#2 The address does not have its own identity; it is embeddable.
#3 There is a one-to-many relationship between <kbd>User</kbd> and <kbd>Bid</kbd>, which is mapped by the <kbd>user</kbd> field on the <kbd>Bid</kbd> side. <kbd>CascadeType.ALL</kbd> indicates that all operations will be propagated from the parent <kbd>User</kbd> to the child <kbd>Bid</kbd>.

The User mapping shows `@Entity` alongside a generated identifier, an embedded value, and a one-to-many relationship. section 19.2.2

The same JPA mappings can be used through Hibernate or Spring Data JPA. Do not confuse the annotation with persistence operations: application code still needs a configured persistence approach to store and load instances. With Hibernate OGM, entities using only JPA annotations can also be persisted to MongoDB or Neo4j by changing dependencies and persistence-unit configuration. section 5.0 section 2.8 section 18.4

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Works through the required identifier, default table and column names, field access, and runtime metadata reading; treats relationship mappings separately.
Examples
Uses an Item mapped to ITEM and a User with an embedded Address and a Bid collection.
What it assumes
Assumes familiarity with Java classes and relational tables; introduces the entity declaration and identifier mapping explicitly.
What you can do after
Build it from scratch: declare an entity with `@Entity` and `@Id`, ready for mapping and persistence through JPA.
How it connects
Connects domain models, value types, database identity, field access, Hibernate, and Spring Data JPA.

Also explained in

Compare how 2 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.