Two foreign keys as primary key
you need an embeddable SocialProfileId like this :
@Embeddable
public class SocialProfileId implements Serializable {
@Column(name = "user_id")
private long userId;
@Column(name = "social_network_id")
private long socialNetworkId;
}
then, your SocialProfile entity will look like this :
@Entity
@Table(name = "social_profile")
public class SocialProfile implements java.io.Serializable {
@EmbeddedId
private SocialProfileId id;
@ManyToOne
@JoinColumn(name="user_id")
public User getUser() {
return user;
}
@ManyToOne
@JoinColumn(name="social_network_id")
public SocialNetwork getSocialNetwork() {
return socialNetwork;
}
}
EDIT sorry, i have mixed annotations on fields and methods on my answer ... never do that ! ;-)
In addition to what Pras has said, we can even move the @ManyToOne inside the @Embeddable class itself so SocialProfileId will look like, where you can clearly see in your Embedded class itself that it referencing another table
@Embeddable
public class SocialProfileId implements Serializable {
@ManyToOne
@JoinColumn(name = "user_id")
private User userId;
@ManyToOne
@JoinColumn(name = "social_network_id")
private SocialNetwork socialNetworkId;
public SocialProfileId(User userId, SocialNetwork socialNetworkId) {
this.userId = userId;
this.socialNetworkId = socialNetworkId;
}
}
And your SocialProfile entity can simply have the embedded class and other fields like email and so on..
@Entity
@Table(name = "social_profile")
public class SocialProfile implements java.io.Serializable {
@EmbeddedId
private SocialProfileId id;
@Column(name="email")
private String email;
}