Developers, Developers, Developers! Maksim Sorokin IT Blog

26Mar/10Off

Compound Key In JPA

Sometimes one needs a compound key for specifying @Entity. TopLink website says that one should provide multiple @Id and @IdClass for a composite primary key. But it actually works without specifing @IdClass too.

For example if we have a Pencil with a composite key consisting of color and thickness:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "pencils")
public class Pencil {

  @Id
  @Column(name = "color")
  private String color;

  @Id
  @Column(name = "thickness")
  private String thickness;

  ...
Comments (0) Trackbacks (0)

Sorry, the comment form is closed at this time.

Trackbacks are disabled.