reset password
Author Message
qma
Posts: 7
Posted 01:52 May 07, 2014 |

Save game to database
Hi
In my Game model class I have two attributes
    @ManyToOne
    private User player1;

 
    @ManyToOne
    private User player2;
When I use the Game model class to generate the database, the database gives me the result:
player1_id integer
player2_id integer
When I do game.setPlayer1(player1) and then try to use the code userDao.saveGame(game) to store game to database, the system gives me error. I guess we cannot directly store User player1 to database. Currently what I think the solution might be:
1.    Add int player1_id and int palyer2_id to Game model class.
or
2.    Write some database query to save data.
Would you give me some advice?
Thank you very much.

cysun
Posts: 2935
Posted 08:34 May 07, 2014 |

The mapping and the db schema are correct. The error is due to something else. Read the error message carefully.

qma
Posts: 7
Posted 16:06 May 07, 2014 |

Problem solved with the help of our TA. Just post the solution here in case someone has the save problem as mine.
1.    Need to include cascade in model class. Such as:
      @ManyToOne(cascade=CascadeType.ALL)
      private User player1;
2.    Make sure the player id is not null when try to save the game and use some code like game.setPlayer1(player1) and        userDao.saveGame(game)