2011-01-31 5 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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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,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(); 데이터베이스에서 새로운 엔티티를 가져와야합니다. 수동으로 업데이트해야하는 이유는 무엇입니까? –

+0

@MaurizioPz : Play Framework가 이것을 관리하는 방법을 잘 모르겠지만, 기본 JPA 이해에서 findAll() 메서드는 동일한 지속성 컨텍스트 내에서 호출되므로로드 된 엔티티는 컨텍스트에 의해 캐시되고 후속 호출은 캐시 된 인스턴스. – axtavt

+0

이제 이해합니다. 감사합니다. –

관련 문제