@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

