Tuesday, March 3, 2015

JPA Composite Key

Create class that will represent composite key and has implemented hashCode and equals methods.

public class MsisdnCompositeKey implements Serializable
{
private static final long serialVersionUID = 1L;

private String msisdn;
private Long providerId;
.....


Class that uses this composite key needs to be annotated with @IdClass and have same fields (msisdn and providedId as in example).

@Entity
@Table(name = "MSISDN")
@IdClass(value = MsisdnCompositeKey.class)
public class Msisdn
{
private static final long serialVersionUID = 1L;

@Id
private String msisdn;
@Id
private Long providerId;

.....

Ref:
https://stackoverflow.com/questions/13032948/how-to-create-and-handle-composite-primary-key-in-jpa

No comments:

Post a Comment