/**
* Test the attach method with update on a collection: make an object persistent, detach it , modify it and then attach it.
*/
public void testAttachModifiedCollectionCopy() {
logger.log(BasicLevel.DEBUG, "***************testAttachModifiedCollectionCopy*****************");
Team t = new Team("Nantes",null,null);
Collection players = new ArrayList();
Player p1 = new Player("p5", t, 20);
players.add(p1);
Player p2 = new Player("p6", t, 30);
players.add(p2);
t.setPlayers(players);
Coach c = new Coach("c3", 10, t);
t.setCoach(c);
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
logger.log(BasicLevel.DEBUG, "make persistent the team " + t.toString());
pm.makePersistent(t);
pm.currentTransaction().commit();
//detach the team t
Team copyOfT = (Team) pm.detachCopy(t);
pm.close();
//modify the coach
copyOfT.getCoach().setExperience(99);
//and modify the collection
Iterator it = copyOfT.getPlayers().iterator();
while(it.hasNext()){
Player p = (Player) it.next();
p.setAge(99);
}
// print the team out
logger.log(BasicLevel.DEBUG, "Copy of team " + copyOfT.toString());
PersistenceManager pm2 = pmf.getPersistenceManager();
try {
pm2.currentTransaction().begin();
//attach the team t
Team attachedTeam = (Team) pm2.makePersistent(copyOfT);
Iterator itP = attachedTeam.getPlayers().iterator();
while(itP.hasNext()){
Player p = (Player) itP.next();
assertEquals(99, p.getAge());
}
pm2.currentTransaction().commit();
logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedTeam.toString());
} catch (Exception e) {
fail(e.getMessage());
} finally {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();