Home > Java > javaTutorial > How to Effectively Map Composite Keys in JPA and Hibernate using `@EmbeddedId` and `@IdClass`?

How to Effectively Map Composite Keys in JPA and Hibernate using `@EmbeddedId` and `@IdClass`?

Susan Sarandon
Release: 2024-12-11 11:20:11
Original
269 people have browsed it

How to Effectively Map Composite Keys in JPA and Hibernate using `@EmbeddedId` and `@IdClass`?

Mapping Composite Keys with JPA and Hibernate

When mapping entities to database tables, it's common to use primary keys to uniquely identify records. Composite keys, consisting of multiple columns, are often used in legacy database systems. Both JPA and Hibernate provide annotations to handle composite keys effectively.

Using EmbeddedId

The EmbeddedId annotation denotes that a Java class represents a composite primary key. It maps the class to the table's clustered primary key. The class must:

  • Be annotated with @Embeddable
  • Define public or protected properties corresponding to the primary key columns
  • Have a public constructor that takes values for all primary key columns
  • Implement equals() and hashCode() methods for value equality

Example with EmbeddedId

@Entity
public class Time {
    @EmbeddedId
    private TimePK timePK;

    private String src;
    private String dst;
    private Integer distance;
    private Integer price;
}

@Embeddable
public class TimePK {
    private Integer levelStation;
    private Integer confPathID;

    // Constructor, equals(), and hashCode() methods omitted for brevity
}
Copy after login

Using IdClass

The IdClass annotation specifies that multiple fields or properties of an entity class make up a composite primary key. The class must:

  • Extend a serializable object (e.g., java.io.Serializable)
  • Have a public no-argument constructor
  • Define public (or protected, with property-based access) fields or properties corresponding to the primary key columns
  • Implement equals() and hashCode() methods for value equality

Example with IdClass

@Entity
@IdClass(TimePK.class)
public class Time {

    @Id
    private Integer levelStation;
    @Id
    private Integer confPathID;

    private String src;
    private String dst;
    private Integer distance;
    private Integer price;
}

public class TimePK implements Serializable {
    private Integer levelStation;
    private Integer confPathID;

    // Constructor, equals(), and hashCode() methods omitted for brevity
}
Copy after login

Choosing Between EmbeddedId and IdClass

EmbeddedId creates a separate class for the composite key, while IdClass uses the entity class itself to hold the primary key fields. EmbeddedId is more explicit and suggests a meaningful entity, while IdClass is better for combinations of fields that don't carry an independent meaning.

References

  • JPA 1.0 specification: https://jcp.org/en/jsr/detail?id=220

    • Section 2.1.4: https://jspec.dev.java.net/nonav/javadoc/javax/persistence/doc-files/javax/persistence/spec/version-1_0-final.doc.html#section_2.1.4
    • Section 9.1.14: https://jspec.dev.java.net/nonav/javadoc/javax/persistence/doc-files/javax/persistence/spec/version-1_0-final.doc.html#section_9.1.14
    • Section 9.1.15: https://jspec.dev.java.net/nonav/javadoc/javax/persistence/doc-files/javax/persistence/spec/version-1_0-final.doc.html#section_9.1.15

The above is the detailed content of How to Effectively Map Composite Keys in JPA and Hibernate using `@EmbeddedId` and `@IdClass`?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template