Composite key in hibernate

It’s done using @Embeddable and @EmbeddedId annotations.

Let’s assume that we are writing an application that manages “WrokspaceTaskHistory” of various clients. Every “WrokspaceTaskHistory” has a unique “WorkspaceTaskHistoryId”.

To identify a WrokspaceTaskHistory uniquely, we need to know its WorkspaceTaskHistoryId . Check out the following workspace_task_history table that contains a composite primary key which includes both the workspace_id and task_id columns –

CREATE TABLE `workspace_task_history` (
  `workspace_id` VARCHAR(255) NOT NULL,
  `task_id` VARCHAR(128) NOT NULL,
  `status` VARCHAR(10) NULL,
  `start_time` VARCHAR(255) NULL,
  `stop_time` VARCHAR(255) NULL,
  CONSTRAINT `workspaceExistsForTaskHistory`
    FOREIGN KEY (`workspace_id`)
    REFERENCES `demo`.`workspace` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

The Java code for representing the Embeddable id for the table/entity is like below

@Embeddable
public class WorkspaceTaskHistoryId implements Serializable {

    @Column(name = "workspace_id") private String workspaceId;
    @Column(name = "task_id") private String taskId;

    public String getWorkspaceId() {
        return workspaceId;
    }

    public void setWorkspaceId(String workspace_id) {
        this.workspaceId = workspace_id;
    }

    public String getTaskId() {
        return taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }
}

Now the usage of the @EmbeddedId to use @Embeddable class is like below

@Entity
@Table(name="workspace_task_history")
public class WorkspaceTaskHistory implements Serializable {
    @EmbeddedId
    WorkspaceTaskHistoryId workspaceTaskHistoryId;
    @Column(name = "status") private String status;
    @Column(name = "start_time") private String startTime;
    @Column(name = "stop_time") private String stopTime;

    public WorkspaceTaskHistory() {
    }

    public WorkspaceTaskHistoryId getWorkspaceTaskHistoryId() {
        return workspaceTaskHistoryId;
    }

    public void setWorkspaceTaskHistoryId(WorkspaceTaskHistoryId workspaceTaskHistoryId) {
        this.workspaceTaskHistoryId = workspaceTaskHistoryId;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getStartTime() {
        return startTime;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    public String getStopTime() {
        return stopTime;
    }

    public void setStopTime(String stopTime) {
        this.stopTime = stopTime;
    }

}

Spring Data JPA repository for the WorksspaceTaskHistory.java/ workspace_task_history.sql


@Repository
public interface WorkspaceTaskHistoryRepository extends CrudRepository<WorkspaceTaskHistory,WorkspaceTaskHistoryId>{
 WorkspaceTaskHistory findByWorkspaceTaskHistoryId(WorkspaceTaskHistoryId workspaceTaskHistoryId);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Doubts? WhatsApp me !