Spring Framework Tutorial

Software Setup and Configuration (STS/Eclipse/IntelliJ)

Core Spring

Spring Annotations

Spring Data

Spring JDBC

Spring Security

Spring Data JPA - @Id Annotation

The @Id annotation in Spring Data JPA (which is actually part of the Java Persistence API (JPA) specification) is used to indicate the primary key of an entity. It is essential for JPA to identify the unique identifier for each persisted entity in the database.

Attributes:

There aren't specific attributes for the @Id annotation itself. However, you'll often use it in conjunction with other annotations that do have attributes.

Common usages:

  1. Simple ID:

    @Entity
    public class Employee {
        @Id
        private Long id;
    
        // Other fields, getters, setters...
    }
    
  2. Auto-Generated ID: Here, the @GeneratedValue annotation is used alongside the @Id annotation to let the database automatically generate and assign the primary key value.

    @Entity
    public class Employee {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)  // For auto-increment columns in databases like MySQL
        private Long id;
    
        // Other fields, getters, setters...
    }
    

    The strategy attribute of @GeneratedValue determines how the value is auto-generated. Common strategies include:

    • GenerationType.IDENTITY: Relies on an auto-incremented database column to generate the primary key.
    • GenerationType.SEQUENCE: Uses a database sequence for generating the value. Requires @SequenceGenerator to be specified.
    • GenerationType.TABLE: Uses a separate table to generate the primary key.
    • GenerationType.AUTO: Let the persistence provider pick the most appropriate strategy for the given database.
  3. Custom Generated ID: For databases that support sequences (like Oracle), you can specify the sequence name.

    @Entity
    public class Employee {
        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "emp_seq")
        @SequenceGenerator(name = "emp_seq", sequenceName = "employee_sequence")
        private Long id;
    
        // Other fields, getters, setters...
    }
    

In Spring Data JPA applications, once an entity's primary key is marked with @Id, you can use the CrudRepository or JpaRepository interfaces to easily perform CRUD operations without needing to manually define SQL queries. The framework will generate the necessary queries based on the entity's configuration.

  1. Using @Id annotation in Spring Data JPA:

    • The @Id annotation is used to indicate the primary key field of an entity in Spring Data JPA.
    // User.java
    import javax.persistence.Entity;
    import javax.persistence.Id;
    
    @Entity
    public class User {
    
        @Id
        private Long id;
    
        // Other fields and methods...
    }
    
  2. @Id annotation properties in Spring Data JPA example:

    • The @Id annotation is often used with a primary key field of type Long and signifies the unique identifier for an entity.
    // Product.java
    import javax.persistence.Entity;
    import javax.persistence.Id;
    
    @Entity
    public class Product {
    
        @Id
        private Long productId;
    
        // Other fields and methods...
    }
    
  3. How to use @Id with Spring Data JPA entities:

    • Apply the @Id annotation to the primary key field within your Spring Data JPA entity.
    // Order.java
    import javax.persistence.Entity;
    import javax.persistence.Id;
    
    @Entity
    public class Order {
    
        @Id
        private Long orderId;
    
        // Other fields and methods...
    }
    
  4. Defining primary keys with @Id annotation in Spring Data JPA:

    • Use the @Id annotation to define the primary key field for an entity.
    // Customer.java
    import javax.persistence.Entity;
    import javax.persistence.Id;
    
    @Entity
    public class Customer {
    
        @Id
        private Long customerId;
    
        // Other fields and methods...
    }
    
  5. Auto-generated IDs with @Id in Spring Data JPA:

    • Combine @Id with @GeneratedValue to have the database automatically generate unique IDs.
    // Product.java
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    
    @Entity
    public class Product {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long productId;
    
        // Other fields and methods...
    }
    
  6. Composite primary keys and @Id annotation in Spring Data JPA:

    • For composite primary keys, use the @IdClass or @EmbeddedId annotations along with @Id.
    // OrderItem.java
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.IdClass;
    
    @Entity
    @IdClass(OrderItemId.class)
    public class OrderItem {
    
        @Id
        private Long orderId;
    
        @Id
        private Long productId;
    
        // Other fields and methods...
    }
    
  7. Customizing ID generation strategies with @Id in Spring Data JPA:

    • Choose different ID generation strategies, such as SEQUENCE or TABLE, by specifying the strategy attribute in @GeneratedValue.
    // Employee.java
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    
    @Entity
    public class Employee {
    
        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE)
        private Long employeeId;
    
        // Other fields and methods...
    }
    
  8. Mapping entities to database IDs with @Id in Spring Data JPA:

    • The @Id annotation maps the entity's field to the primary key column in the database.
    // Product.java
    import javax.persistence.Entity;
    import javax.persistence.Id;
    
    @Entity
    public class Product {
    
        @Id
        private Long productId;
    
        // Other fields and methods...
    }