public void edit(Coursetag coursetag) throws IllegalOrphanException, NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Coursetag persistentCoursetag = em.find(Coursetag.class, coursetag.getId());
Courses courseIdOld = persistentCoursetag.getCourseId();
Courses courseIdNew = coursetag.getCourseId();
Tag tagIdOld = persistentCoursetag.getTagId();
Tag tagIdNew = coursetag.getTagId();
List<Tagrates> tagratesListOld = persistentCoursetag.getTagratesList();
List<Tagrates> tagratesListNew = coursetag.getTagratesList();
List<String> illegalOrphanMessages = null;
for (Tagrates tagratesListOldTagrates : tagratesListOld) {
if (!tagratesListNew.contains(tagratesListOldTagrates)) {
if (illegalOrphanMessages == null) {
illegalOrphanMessages = new ArrayList<String>();
}
illegalOrphanMessages.add("You must retain Tagrates " + tagratesListOldTagrates + " since its courseTagId field is not nullable.");
}
}
if (illegalOrphanMessages != null) {
throw new IllegalOrphanException(illegalOrphanMessages);
}
if (courseIdNew != null) {
courseIdNew = em.getReference(courseIdNew.getClass(), courseIdNew.getCourseId());
coursetag.setCourseId(courseIdNew);
}
if (tagIdNew != null) {
tagIdNew = em.getReference(tagIdNew.getClass(), tagIdNew.getId());
coursetag.setTagId(tagIdNew);
}
List<Tagrates> attachedTagratesListNew = new ArrayList<Tagrates>();
for (Tagrates tagratesListNewTagratesToAttach : tagratesListNew) {
tagratesListNewTagratesToAttach = em.getReference(tagratesListNewTagratesToAttach.getClass(), tagratesListNewTagratesToAttach.getId());
attachedTagratesListNew.add(tagratesListNewTagratesToAttach);
}
tagratesListNew = attachedTagratesListNew;
coursetag.setTagratesList(tagratesListNew);
coursetag = em.merge(coursetag);
if (courseIdOld != null && !courseIdOld.equals(courseIdNew)) {
courseIdOld.getCoursetagList().remove(coursetag);
courseIdOld = em.merge(courseIdOld);
}
if (courseIdNew != null && !courseIdNew.equals(courseIdOld)) {
courseIdNew.getCoursetagList().add(coursetag);
courseIdNew = em.merge(courseIdNew);
}
if (tagIdOld != null && !tagIdOld.equals(tagIdNew)) {
tagIdOld.getCoursetagList().remove(coursetag);
tagIdOld = em.merge(tagIdOld);
}
if (tagIdNew != null && !tagIdNew.equals(tagIdOld)) {
tagIdNew.getCoursetagList().add(coursetag);
tagIdNew = em.merge(tagIdNew);
}
for (Tagrates tagratesListNewTagrates : tagratesListNew) {
if (!tagratesListOld.contains(tagratesListNewTagrates)) {
Coursetag oldCourseTagIdOfTagratesListNewTagrates = tagratesListNewTagrates.getCourseTagId();
tagratesListNewTagrates.setCourseTagId(coursetag);
tagratesListNewTagrates = em.merge(tagratesListNewTagrates);
if (oldCourseTagIdOfTagratesListNewTagrates != null && !oldCourseTagIdOfTagratesListNewTagrates.equals(coursetag)) {
oldCourseTagIdOfTagratesListNewTagrates.getTagratesList().remove(tagratesListNewTagrates);
oldCourseTagIdOfTagratesListNewTagrates = em.merge(oldCourseTagIdOfTagratesListNewTagrates);
}
}
}
em.getTransaction().commit();