The @IdClass annotation tells JPA that an entity has a composite primary key made of several @Id fields, and that a separate plain class holds those same fields so the persistence provider can represent the whole key as one object. The entity keeps each key field as a normal attribute, and the id class mirrors their names and types exactly.
Use it when a table is identified by two or more columns, such as an order line identified by order_id and line_number. The alternative is @EmbeddedId, which nests the key inside the entity. Both are part of Jakarta Persistence (formerly JPA), and both work with Hibernate 6 and Spring Data JPA.
@IdClass@IdClass with Spring Data JPAA single @Id field maps to one column, and JPA can use its type directly for EntityManager.find(Class, Object). With a composite key there is no single value to pass, so the provider needs a class that groups the key parts. That class is the primary key class, and @IdClass is one of the two ways to declare it.
Typical cases are legacy schemas, link tables with extra columns, and tables that use natural keys such as (country_code, tax_number).
The Jakarta Persistence specification puts these requirements on a primary key class:
public and have a public or protected no-argument constructor.Serializable.equals() and hashCode() using all key fields.@Id attributes in the entity exactly.A Java record is a convenient way to meet the equality and serialization rules, but support for records as id classes depends on the provider and version, because records have no no-argument constructor. Hibernate 6.x accepts records as @IdClass, but check your version before relying on it. A small conventional class is the safest choice and is shown below.
@IdClassThe following example models an order line identified by the order id and a line number.
The entity declares orderId and lineNumber with @Id, and @IdClass(OrderItemId.class) links them to the key class. The id class carries no annotations, because the mapping lives on the entity. Put the two public types in separate files in a real project.
Because the id class is a separate object, you build one to look up an entity.
In JPQL you reference the entity fields directly, since they are ordinary attributes:
This is the main practical advantage of @IdClass over @EmbeddedId: the key parts are top-level attributes, so queries read as i.orderId rather than i.id.orderId.
@IdClass with Spring Data JPAA repository uses the id class as its second type parameter.
findById(new OrderItemId(1001L, 2)) then works as for any other entity. Derived query methods use the entity attribute names, so findByOrderId needs no special handling.
| Aspect | @IdClass | @EmbeddedId |
|---|---|---|
| Key fields live in | The entity, repeated in the id class | The embeddable class only |
| Id class annotations | None | @Embeddable |
| JPQL access | e.orderId | e.id.orderId |
| Duplication | Field names declared twice | Declared once |
| Best for | Legacy schemas, flat entity access | New code that treats the key as one value |
Choose @EmbeddedId when you want to pass the key around as a single value object. Choose @IdClass when the key parts are meaningful attributes on their own or when you must keep an existing entity shape.
equals() and hashCode(), which breaks the persistence context cache and can produce duplicate or lost entities.javax.persistence imports with Hibernate 6 and Spring Boot 3, which use the jakarta.persistence package instead.@IdClass required for a composite key?No. You can use @EmbeddedId instead. Exactly one of the two approaches is used per entity, not both.
No. It is a plain serializable class with no annotations when used with @IdClass. Only the entity carries mapping annotations.
Yes, a key part can be a @ManyToOne association marked with @Id, which is called a derived identity. In the id class the matching field holds the type of the referenced entity's primary key. @MapsId with @EmbeddedId is often simpler for this case.
find return null with my id class?The usual causes are a wrong value in one key field or missing equals() and hashCode(). Check that every key field is set and that the id class uses all of them.
@IdClass maps a composite primary key by pairing an entity that declares several @Id fields with a plain Serializable class that mirrors them. Keep the names and types identical, implement equals() and hashCode(), and use the jakarta.persistence imports on current stacks. Prefer @EmbeddedId for new designs where the key is a single concept, and use @IdClass when flat attribute access matters.