What is a composite key?
sobes.tech AI
Answer from AI
A composite (or concatenated) key in a database is a primary key consisting of two or more columns of a table. The uniqueness of a record is determined by the combination of values of all columns included in the composite key.
In Java applications using JPA, a composite key is usually implemented in one of two ways:
-
Using
@Embeddableand@EmbeddedId:A separate class marked with the
@Embeddableannotation is created, which encapsulates the fields that make up the composite key. In the entity, this class is embedded using the@EmbeddedIdannotation. The class representing the composite key must implementSerializableand override theequals()andhashCode()methods.// Composite key class @Embeddable public class OrderItemId implements Serializable { private Long orderId; private Long productId; // Constructors, getters, setters @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; OrderItemId that = (OrderItemId) o; return Objects.equals(orderId, that.orderId) && Objects.equals(productId, that.productId); } @Override public int hashCode() { return Objects.hash(orderId, productId); } } // Entity with composite key @Entity public class OrderItem { @EmbeddedId private OrderItemId id; private int quantity; // Constructors, getters, setters } -
Using
@IdClass:A separate class representing the composite key (also implementing
Serializableand overridingequals()andhashCode()) is created, and the entity specifies the name of this class using the@IdClassannotation. The fields that are part of the composite key are declared directly in the entity and marked with the@Idannotation. The field names in the entity and the key class must match.// Composite key class public class OrderItemId implements Serializable { private Long orderId; private Long productId; // Constructors, getters, setters @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; OrderItemId that = (OrderItemId) o; return Objects.equals(orderId, that.orderId) && Objects.equals(productId, that.productId); } @Override public int hashCode() { return Objects.hash(orderId, productId); } } // Entity with composite key @Entity @IdClass(OrderItemId.class) public class OrderItem { @Id private Long orderId; @Id private Long productId; private int quantity; // Constructors, getters, setters }