Two foreign keys as primary key

I have the following tables

enter image description here

How would I implement this using sleeping annotations?

Current code: (cut for brevity)

user

@Entity @Table(name = "user") public class User implements java.io.Serializable { @Id @GeneratedValue public Long getId() { return id; } } 

Social network

 @Entity @Table(name = "social_network") public class SocialNetwork implements java.io.Serializable { @Id @GeneratedValue public int getId() { return id; } } 

SocialProfile

 @Entity @Table(name = "social_profile") public class SocialProfile implements java.io.Serializable { @Id @ManyToOne @JoinColumn(name="user_id") public User getUser() { return user; } @Id @ManyToOne @JoinColumn(name="social_network_id") public SocialNetwork getSocialNetwork() { return socialNetwork; } } 

Obviously my code is not working right now. Can anyone shed some light on this?

+8
source share
4 answers

you will need the embeddable SocialProfileId:

 @Embeddable public class SocialProfileId implements Serializable { @Column(name = "user_id") private long userId; @Column(name = "social_network_id") private long socialNetworkId; } 

then your SocialProfile object 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 for the fields and methods of my answer ... never do this !; -)

+7
source

It seems you need a mamy-to-many mapping having a join table with an extra column. To do this, you need to create 2 more classes.

FYI: http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/

0
source

My problem for a REST application was similar, but a little different, I will post it here. I had 2 data tables: Song and Tag with identifier each (songid, tagid). Then I had a table for joining them together Tagassignment , in which there were only both main keys: Song and Tag . Therefore, I did not want to join them, I wanted to save a table with both foreign keys.

Source of my solution: http://www.objectdb.com/java/jpa/entity/id


Front

Song

 @Entity @Table(name = "songs") data class Song( @Id @GeneratedValue(strategy = GenerationType.AUTO) val id: Int, ... ) 

Tag

 @Entity @Table(name = "tags") data class Tag( @Id @GeneratedValue(strategy = GenerationType.AUTO) val id: Int, ... ) 

Tagassignment

  @Entity @Table(name = "tagassignment") data class Tagassignment( val songid: Int, val tagid: Int ) 

After

I did not change the Song and Tag .

Tagassignment

 @Entity @IdClass(value = TagassignmentKey::class) @Table(name = "tagassignment") data class Tagassignment( @Id val songid: Int, @Id val tagid: Int ) 

and I created a Key class

Tagassignmentkey

 class TagassignmentKey(val songid: Int, val tagid: Int) : Serializable { constructor() : this(0, 0) } 
0
source

In addition to what Pras said, we can even move @ManyToOne inside the @Embeddable class itself so that SocialProfileId looks like where you can clearly see in the Embedded class itself that it refers to 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 may just have an inline class and other fields like email, etc.

 @Entity @Table(name = "social_profile") public class SocialProfile implements java.io.Serializable { @EmbeddedId private SocialProfileId id; @Column(name="email") private String email; } 
0
source

All Articles