Skip to main content

Composite Primary Keys in Spring Boot JPA

Composite Primary Keys in Spring Boot JPA

In Spring Boot, the Java Persistence API (JPA) provides an interface for working with databases. The use of a primary key in a database is a common practice to uniquely identify a record. However, there are cases where a single primary key may not be sufficient, and a composite primary key may be required. In this blog, we will discuss how to use composite primary keys in Spring Boot JPA.

Defining a Composite Primary Key

To define a composite primary key, we need to create a class that represents the primary key. This class should contain fields that correspond to the primary key columns in the database table. The class should also implement the Serializable interface and define equals() and hashCode() methods. Here's an example:

public class EmployeeId implements Serializable {
  private Long departmentId;
  private Long employeeId;
  
  // Constructors, getters and setters, equals(), and hashCode() methods
}

Here, we've defined a composite primary key for an Employee entity that consists of two columns: departmentId and employeeId. The EmployeeId class implements Serializable and defines the necessary methods.

Using a Composite Primary Key in an Entity

Once we've defined the composite primary key class, we can use it in the entity that corresponds to the database table. We need to annotate the primary key field or fields with the @EmbeddedId annotation and specify the name of the primary key class. Here's an example:

public class Employee {
  @EmbeddedId
  private EmployeeId id;
  
  // Other entity fields and methods
}

Here, we've annotated the id field with @EmbeddedId and specified the EmployeeId class as the primary key class. This tells JPA that the id field is a composite primary key.

Querying with a Composite Primary Key

When querying for an entity with a composite primary key, we need to create an instance of the primary key class and set its fields to the values we're looking for. We can then use the EntityManager's find() method to retrieve the entity. Here's an example:

EmployeeId id = new EmployeeId(1L, 2L);
Employee employee = entityManager.find(Employee.class, id);

Here, we've created an instance of the EmployeeId class with departmentId = 1 and employeeId = 2. We then use the EntityManager's find() method to retrieve the corresponding Employee entity.

Conclusion

Composite primary keys can be useful when a single primary key is not sufficient to uniquely identify a record in a database table. In Spring Boot JPA, we can define a composite primary key by creating a separate class and annotating the primary key field or fields with @Embedded Id. We can then use this composite primary key in our entity and query for records using an instance of the primary key class.

Code

Here's the complete code for the Employee and EmployeeId classes:

public class EmployeeId implements Serializable {


private Long departmentId;
private Long employeeId;

// Constructors, getters and setters, equals(), and hashCode() methods
}

@Entity
public class Employee {
@EmbeddedId
private EmployeeId id;
private String name;

// Constructors, getters and setters
}

You can also check out the following example project on GitHub that demonstrates the usage of composite primary keys in Spring Boot JPA:

https://github.com/sudeepcv/CompositePrimaryKeys

We hope this blog post has been helpful in understanding how to use composite primary keys in Spring Boot JPA. Happy coding!

Comments