SQL JPA – Multiple columns as primary key

You need to have a class for your composite key:

public class CompositeKey implements Serializable {
    private int column1;
    private int column2;
    private int column3;
}

and then in your entity class use the @IdClass annotation:

@Entity
@IdClass(CompositeKey.class)
public class EntityExample {
    @Id
    private int column1;
    @Id
    private int column2;
    @Id
    private int column3;
    ...
    ...
}

I think this should work.

There is also the other solution that @jklee mentioned. Both work, it’s a matter of preference.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)