2011-01-31 18 views
1

嗨我的项目有问题。我有一个后台作业 运行它更新一个实体。问题是,如果我从控制器更新 另一个实体,第一个实体不会对 更新的实体做出反应。但是,如果我尝试访问从控制器的第二 实体我可以看到,它进行了更新,实体在受控更新中未更新的作业

下面的代码: 第二实体 包款;

import javax.persistence.Entity; 

import play.db.jpa.Model; 
@Entity 
public class GameMap extends Model { 
    static int convPx=16; //converts game pixels in the map rap. 
    int[][] array; 
    public GameMap(int[][] array) { 
     this.array=array; 
    } 

    public boolean isSolid(int x,int y){ 
     try{ 
     if (array[y][x]==0) 
      return false; 
     else 
      return true; 
     }catch(Exception e){ 
      return true; 
     } 
    } 
    public String collides(GameObject object){  //TODO fix if it collides top and left scenario 
     if(isSolid((object.x+object.width/2)/convPx,object.y/convPx)) 
      return "top"; 
     if(isSolid((object.x+object.width/2)/convPx,(object.y+object.height)/ 
convPx)) 
      return "bottom"; 
     if(isSolid((object.x)/convPx,(object.y+object.height/2)/convPx)) 
      return "left"; 
     if(isSolid((object.x+object.width)/convPx,(object.y+object.height/2)/ 
convPx)) 
      return "right"; 
     else 
      return "false"; 
    } 

    public void addPoint(int x, int y, int what) { 
     array[x/convPx][y/convPx]=what; 
     this.save(); 
    } 
} 

更新我从这个控制器

public static void addMapPoint(int x, int y){ 
      GameMap map =(GameMap) GameMap.findAll().iterator().next(); 
      map.addPoint(x, y, 1); 

    } 

这调用appPoint()方法是没有更新

包装控制器的工作;

import java.util.Iterator; 
import java.util.List; 

import models.Ball; 
import models.GameMap; 
import play.jobs.Job; 

public class BallJob extends Job { 
    public void doJob() { 
     List<Ball> balls; 
     try { 
      Thread.sleep(1000); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     new Ball(40,40,4,4,10,10).save(); 
     int[][] mapArr = { 

{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 

{1,0,0,0,0,0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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 

{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 

{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 

{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} 
     }; 
     GameMap m= new GameMap(mapArr); 
     m.save(); 
     Long time = System.currentTimeMillis(); 
     while (true){ 
     try { 
      if(25-System.currentTimeMillis()+time>0){ 
       Thread.sleep(25-System.currentTimeMillis()+time); 
      } 
       time =System.currentTimeMillis(); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     balls = Ball.findAll(); 
/*************************************************************** 
**************************************************************** 
* This map doesn't change 
**************************************************************** 
***************************************************************/ 
     GameMap map = (GameMap) GameMap.findAll().iterator().next(); 

     for (Iterator iterator = balls.iterator(); 
iterator.hasNext();) { 
      Ball ball = (Ball) iterator.next(); 
      ball.applyForces(); 
      if(ball.collides(map)){ 
      ball.applyForces(); 
      } 

     } 
     } 
    } 

} 

任何想法? 谢谢

回答

1

JPA不会将更改从数据库传播到现有实体,从而进入持久性上下文。我想你需要刷新它:

map.refresh(); 
+0

这工作,但我不明白为什么。这条线:GameMap map =(GameMap)GameMap.findAll()。iterator()。next();我应该从数据库中获得一个新实体,为什么我需要手动更新它? – 2011-01-31 13:00:10