public void edit(Faction faction) throws IllegalOrphanException, NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Faction persistentFaction = em.find(Faction.class, faction.getId());
Collection<Factionstat> factionstatCollectionOld = persistentFaction.getFactionstatCollection();
Collection<Factionstat> factionstatCollectionNew = faction.getFactionstatCollection();
List<String> illegalOrphanMessages = null;
for (Factionstat factionstatCollectionOldFactionstat : factionstatCollectionOld) {
if (!factionstatCollectionNew.contains(factionstatCollectionOldFactionstat)) {
if (illegalOrphanMessages == null) {
illegalOrphanMessages = new ArrayList<String>();
}
illegalOrphanMessages.add("You must retain Factionstat " + factionstatCollectionOldFactionstat + " since its faction field is not nullable.");
}
}
if (illegalOrphanMessages != null) {
throw new IllegalOrphanException(illegalOrphanMessages);
}
Collection<Factionstat> attachedFactionstatCollectionNew = new ArrayList<Factionstat>();
for (Factionstat factionstatCollectionNewFactionstatToAttach : factionstatCollectionNew) {
factionstatCollectionNewFactionstatToAttach = em.getReference(factionstatCollectionNewFactionstatToAttach.getClass(), factionstatCollectionNewFactionstatToAttach.getFactionstatPK());
attachedFactionstatCollectionNew.add(factionstatCollectionNewFactionstatToAttach);
}
factionstatCollectionNew = attachedFactionstatCollectionNew;
faction.setFactionstatCollection(factionstatCollectionNew);
faction = em.merge(faction);
for (Factionstat factionstatCollectionNewFactionstat : factionstatCollectionNew) {
if (!factionstatCollectionOld.contains(factionstatCollectionNewFactionstat)) {
Faction oldFactionOfFactionstatCollectionNewFactionstat = factionstatCollectionNewFactionstat.getFaction();
factionstatCollectionNewFactionstat.setFaction(faction);
factionstatCollectionNewFactionstat = em.merge(factionstatCollectionNewFactionstat);
if (oldFactionOfFactionstatCollectionNewFactionstat != null && !oldFactionOfFactionstatCollectionNewFactionstat.equals(faction)) {
oldFactionOfFactionstatCollectionNewFactionstat.getFactionstatCollection().remove(factionstatCollectionNewFactionstat);
oldFactionOfFactionstatCollectionNewFactionstat = em.merge(oldFactionOfFactionstatCollectionNewFactionstat);
}
}
}
em.getTransaction().commit();