SQL JPA - Multiple columns as primary key

Use @Embeddable and @EmbeddedId.

Example:

@Entity
public class Project implements Serializable {
    @EmbeddedId ProjectId id;
}
 
@Embeddable
class ProjectId implements Serializable {
    int departmentId;
    long projectId;
}

More information here http://www.objectdb.com/java/jpa/entity/id#Embedded_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. Hope it helps, cheers!

Yea and there is the other solution, the one that @jklee mentioned, both work, it's a matter of preference.