liveBook · Manning

@Id

As explained in Spring in Action, Sixth Edition

@Id is a Spring Data JDBC annotation that marks a domain-property field as the persistent identity of an object.

@Id marks a domain property as the persistent identity of an object. The persistence framework uses that property to recognize the object’s database row or document. section 3.2.3 section 4.2.2

Use @Id according to the persistence technology:

  • Spring Data JDBC uses org.springframework.data.annotation.Id on identity properties such as TacoOrder.id, Taco.id, and Ingredient.id. section 3.2.3
  • JPA uses javax.persistence.Id with @Entity; generated numeric identifiers commonly combine @Id with @GeneratedValue. section 3.3.2
  • Spring Data MongoDB uses org.springframework.data.annotation.Id to designate a document identifier, including String or Long properties. section 4.2.2
  • Annotate each domain type that needs an identity, but omit @Id for identity-free relational structures such as IngredientRef. section 3.2.3

Identity properties in domain classes

In Spring Data JDBC, a persisted order can declare `@Id private Long id;`. An ingredient can use `@Id private String id;`. In JPA, the same identity declaration uses `javax.persistence.Id`, while MongoDB places Spring Data’s `@Id` on a document property. section 3.2.3 section 3.3.2 section 4.2.2

Listing 3.15 Preparing the Taco class for persistence
package tacos;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import javax.validation.constraints.Digits;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
 
import org.hibernate.validator.constraints.CreditCardNumber;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;
 
import lombok.Data;
 
@Data
@Table
public class TacoOrder implements Serializable {
 
  private static final long serialVersionUID = 1L;
 
  @Id
  private Long id;
 
 // ...
 
}

View a complete TacoOrder class showing the Spring Data JDBC import, @Table mapping, and @Id annotation on the Long identity field. section 3.2.3

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains placement, supported identifier examples, and namespace differences, but does not detail identifier-generation internals.
Examples
Uses TacoOrder, Taco, Ingredient, and IngredientRef classes with Long or String identity properties.
What it assumes
Assumes domain classes and persistence mappings, while distinguishing framework-specific annotation packages.
What you can do after
Apply the correct @Id variant to identity properties in Spring Data JDBC, JPA, or MongoDB domain types.
How it connects
Connects persistent identity with Spring Data JDBC, JPA, MongoDB, @Table, @Entity, @Document, and @GeneratedValue.

Also explained in

Compare how 2 books explain @Id →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.