You should create a new @Embeddable class containing PK fields:
@Embeddable public class user_groupId implements Serializable { @Column(name="userId") private String userId; @Column(name="groupId") private String group; }
And use it in @Entity as @EmbeddedId :
@Entity public class user_group { @EmbeddedId user_groupId id; ... }
You can also use the @IdClass annotation.
This excellent answer from Pascal Thivent details the details. You can also take a look at this other answer, which I posted on an almost identical question a while ago.
As a side note, if you have control over the structure of the database, you can also consider excluding composite keys. There are several reasons for this .
Xavi Lรณpez
source share