2012-07-04 64 views
0

从来就得到了这个父对象JPA,合并对象更新儿童

@OneToMany(mappedBy="idUser", cascade = CascadeType.MERGE) 
public List<Directions> directions; 

从来就上,并在我的控制器得到这个

public static void userUpdate(String apikey, JsonObject body) { 
    if(validate(apikey)) { 

     Long idUser = decode(apikey); 
     User oldUser = User.findById(idUser); 

     Map<String, User> userMap = new HashMap<String, User>(); 
     Type arrayListType = new TypeToken<Map<String, User>>(){}.getType(); 
     userMap = gson().fromJson(body, arrayListType); 
     User user = userMap.get("user"); 

     oldUser.em().merge(user); 

     oldUser.save(); 

    }else{ 
     forbidden(); 
    } 
} 

这使得父对象上的更新,但当我更改子对象上的某些东西时,它不会更新它,也不会给hibernate或Oracle带来问题。

有谁知道为什么它不会更新子对象?

谢谢大家!

更新了解决方案!

这就是它对我的作用,因为@JB Nizet说你也必须保存子对象。

oldUser.em().merge(user); 
oldUser.save(); 

for (Direction direction : oldUser.directions) { 
    direction.save();   
} 

另一种说法!

由于能够使oldUser.save(),并得到挽救了孩子的对象这

@OneToMany(cascade = CascadeType.ALL) 
@JoinColumn(name = "SASHNBR", insertable = true, updatable = true) 
public List<Direction> directions; 

从来就。

+1

难道你看http://stackoverflow.com/questions/2530498/jpa-persisting-object-parent-is-ok-但孩子没有更新 – rayyildiz

+0

@rayyildiz我做了,如果你看看我实现CascadeType.MERGE这是我想要的代码。无论如何,我会尝试与CascadeType.ALL。谢谢! :) – axierjhtjz

+0

@rayyildiz也没有工作。 :( – axierjhtjz

回答

1

AFAIK,Play需要在所有修改的实体上调用save()。所以,你可能需要通过用户的方向迭代,并保存它们:

for (Direction direction : user.getDirections()) { 
    direction.save(); 
} 
+0

感谢您的回答,这是所有我需要更多的细节: \t \t \t oldusuario.em()合并(usuario); \t \t \t \t \t \t oldusuario.save(); 。 \t \t \t \t \t为(Direccion direccion:oldusuario.direccion){ \t \t \t direccion.save(); \t \t \t} – axierjhtjz