Developers, Developers, Developers! Maksim Sorokin IT Blog


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;

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

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

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

Comments (0) Trackbacks (0)

Sorry, the comment form is closed at this time.

Trackbacks are disabled.