gpt4 book ai didi

java - 在受控对象中更新的实体未在作业中更新

转载 作者:太空宇宙 更新时间:2023-11-04 08:43:14 25 4
gpt4 key购买 nike

您好,我的项目遇到问题。我有一份后台工作运行更新一个实体。问题是如果我更新来自 Controller 的另一个实体,第一个实体不会对此使用react更新的实体。但是如果我尝试从第二个 Controller 访问实体我可以看到它已更新,

代码如下:第二实体封装模型;

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();
}
}

我从该 Controller 调用 appPoint() 方法来更新它

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

}

这是未更新的职位

封装 Controller ;

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,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,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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();
}

}
}
}

}

有什么想法吗?谢谢

最佳答案

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

map.refresh();

关于java - 在受控对象中更新的实体未在作业中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4849813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com