java - JPA modelling, one-to-one relation? -
i new jpa , stuggles defining relations between classes. have class called player , class called game. game holds references 2 player instances. question is, how should modelled?
this current code:
@entity @table(name = "t_player") @jsonserialize(include=jsonserialize.inclusion.non_null) public class player { @id @generatedvalue(strategy = generationtype.identity) private long id; @basic @column(name = "name") private string name; @basic @column(name = "uuid") private final string uuid = uuid.randomuuid().tostring();
i think ok, problem in game class:
@entity @table(name = "t_game") @jsonserialize(include=jsonserialize.inclusion.non_null) public class game { public game() { } @id @generatedvalue(strategy = generationtype.identity) private long id; @basic @column(name = "uuid") private final string uuid = uuid.randomuuid().tostring(); @onetoone @primarykeyjoincolumn @joincolumn(name = "id") private player player_1; @onetoone @primarykeyjoincolumn @joincolumn(name = "player_2") private player player_2; public game(player player_1, player player_2) { this.player_1 = player_1; this.player_2 = player_2; } }
this not working, table t_game has 2 field; id , uuid. problem?
remove primarykeyjoincolumn annotation, don't think meant use, conflicts joincolumn definition. use joincolumn annotation instead define foreign key field name , field references if necessary.
Comments
Post a Comment