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) }
source share