A composite key in Hibernate is a primary key made up of more than one column, mapped in Java by grouping those columns into a dedicated key class rather than a single field.
An OrderItem table keyed by (order_id, product_id) can be modeled with an OrderItemId class holding those two fields, embedded into OrderItem via @EmbeddedId, so orderItem.getId().getProductId() retrieves part of the key.
@Embeddable
public class OrderItemId implements Serializable {
private Long orderId;
private Long productId;
// equals(), hashCode(), getters/setters
}
@Entity
public class OrderItem {
@EmbeddedId
private OrderItemId id;
}A concise interview answer is
A composite key is a primary key spanning multiple columns, and in Hibernate I model it with a separate Serializable class marked @Embeddable and referenced from the entity with @EmbeddedId, or alternatively with @IdClass — either way that key class needs correct equals() and hashCode() implementations.
Last updated: 11 November 2026
Previous question: What is cascading in Hibernate?
Next question: How does Hibernate protect against SQL injection?