Skip to content

Instantly share code, notes, and snippets.

@rastadrian
Last active October 12, 2017 16:50
Show Gist options
  • Save rastadrian/6db1adb18ad1ab4099ba947a3e599921 to your computer and use it in GitHub Desktop.
Save rastadrian/6db1adb18ad1ab4099ba947a3e599921 to your computer and use it in GitHub Desktop.
JPA Composite Key Example
@Getter
@Setter
@Entity
@IdClass(MyEntity.CompositeKey.class)
@Table(name = "ENTITY_TABLE")
public class MyEntity implements Serializable {
@Id
@Column(name = "FIRST_ID", nullable = false)
private String firstId;
@Id
@Column(name = "SECOND_ID", nullable = false)
private String secondId;
@Id
@Column(name = "THIRD_ID", nullable = false)
private String thirdId;
@Column(name = "STRING_FIELD")
private String stringField;
@Column(name = "INT_FIELD")
private Integer intField;
@Getter
@Setter
public static class CompositeKey implements Serializable {
String firstId;
String secondId;
String thirdId;
}
}
@Repository
public interface MyEntityRepository extends CrudRepository<MyEntity, MyEntity.CompositeKey> {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment