Q14. How do you map a composite primary key?
Two options. @EmbeddedId uses a single @Embeddable class that holds all the key fields, and you access the key as one object. @IdClass keeps the key fields directly on the entity and names a separate class that mirrors them for equality and hashing.
@EmbeddedId is usually cleaner because the key is one cohesive object. Either way, the key class must implement equals() and hashCode() and be serializable.
@Embeddable
public class OrderLineId implements Serializable {
private Long orderId;
private Long productId;
// equals() and hashCode() required
}
@Entity
public class OrderLine {
@EmbeddedId
private OrderLineId id;
}