lundi 29 juin 2015

JPA+EJB not managed entity in websocket communication

  1. Browser and server connected with websocket
  2. After connected, browser will send data, and handled in server by method 'firstMessage'. This method will store Player entity to variable named 'player' and change its data (it work fine, the database is change )
  3. Next, the browser send data again handled in server by method 'secondMessage'. It change the Player entity data that already stored before. the data is change, but the problem is the database not changing

    @Singleton
    @Startup
    public class Engine {
    
    @Inject DaoPlayer daoPlayer;
    Player player;    
    
    public void firstMessage(clientId){
        player = daoPlayer.findById(clientId);
        player.coin = 3;
    }
    
    public void secondMessage(){
        player.coin = 999;
    
    

    }

Problem :

Change in Player entity inside method 'secondMessage' not updating the database

What i try so far :

  • I already check class Engine, DaoPlayer and Player is the same instance (by generating random number when they created)
  • i do this in secondMessage and it work fine. but i dont want to this.

    public void secondMessage(){
        player.coin = 999;
        daoPlayer.getEntityManager().merge(player);
    
    

    }

    i expecting

    player.coin = 999; should be enough to update the database

Aucun commentaire:

Enregistrer un commentaire