public void edit(Enrollment enrollment) throws IllegalOrphanException, NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Enrollment persistentEnrollment = em.find(Enrollment.class, enrollment.getId());
Courserates courseratesOld = persistentEnrollment.getCourserates();
Courserates courseratesNew = enrollment.getCourserates();
User userIdOld = persistentEnrollment.getUserId();
User userIdNew = enrollment.getUserId();
Courses courseIdOld = persistentEnrollment.getCourseId();
Courses courseIdNew = enrollment.getCourseId();
List<Comments> commentsListOld = persistentEnrollment.getCommentsList();
List<Comments> commentsListNew = enrollment.getCommentsList();
List<String> illegalOrphanMessages = null;
if (courseratesOld != null && !courseratesOld.equals(courseratesNew)) {
if (illegalOrphanMessages == null) {
illegalOrphanMessages = new ArrayList<String>();
}
illegalOrphanMessages.add("You must retain Courserates " + courseratesOld + " since its enrollment field is not nullable.");
}
for (Comments commentsListOldComments : commentsListOld) {
if (!commentsListNew.contains(commentsListOldComments)) {
if (illegalOrphanMessages == null) {
illegalOrphanMessages = new ArrayList<String>();
}
illegalOrphanMessages.add("You must retain Comments " + commentsListOldComments + " since its enrollmentId field is not nullable.");
}
}
if (illegalOrphanMessages != null) {
throw new IllegalOrphanException(illegalOrphanMessages);
}
if (courseratesNew != null) {
courseratesNew = em.getReference(courseratesNew.getClass(), courseratesNew.getId());
enrollment.setCourserates(courseratesNew);
}
if (userIdNew != null) {
userIdNew = em.getReference(userIdNew.getClass(), userIdNew.getId());
enrollment.setUserId(userIdNew);
}
if (courseIdNew != null) {
courseIdNew = em.getReference(courseIdNew.getClass(), courseIdNew.getCourseId());
enrollment.setCourseId(courseIdNew);
}
List<Comments> attachedCommentsListNew = new ArrayList<Comments>();
for (Comments commentsListNewCommentsToAttach : commentsListNew) {
commentsListNewCommentsToAttach = em.getReference(commentsListNewCommentsToAttach.getClass(), commentsListNewCommentsToAttach.getId());
attachedCommentsListNew.add(commentsListNewCommentsToAttach);
}
commentsListNew = attachedCommentsListNew;
enrollment.setCommentsList(commentsListNew);
enrollment = em.merge(enrollment);
if (courseratesNew != null && !courseratesNew.equals(courseratesOld)) {
Enrollment oldEnrollmentOfCourserates = courseratesNew.getEnrollment();
if (oldEnrollmentOfCourserates != null) {
oldEnrollmentOfCourserates.setCourserates(null);
oldEnrollmentOfCourserates = em.merge(oldEnrollmentOfCourserates);
}
courseratesNew.setEnrollment(enrollment);
courseratesNew = em.merge(courseratesNew);
}
if (userIdOld != null && !userIdOld.equals(userIdNew)) {
userIdOld.getEnrollmentList().remove(enrollment);
userIdOld = em.merge(userIdOld);
}
if (userIdNew != null && !userIdNew.equals(userIdOld)) {
userIdNew.getEnrollmentList().add(enrollment);
userIdNew = em.merge(userIdNew);
}
if (courseIdOld != null && !courseIdOld.equals(courseIdNew)) {
courseIdOld.getEnrollmentList().remove(enrollment);
courseIdOld = em.merge(courseIdOld);
}
if (courseIdNew != null && !courseIdNew.equals(courseIdOld)) {
courseIdNew.getEnrollmentList().add(enrollment);
courseIdNew = em.merge(courseIdNew);
}
for (Comments commentsListNewComments : commentsListNew) {
if (!commentsListOld.contains(commentsListNewComments)) {
Enrollment oldEnrollmentIdOfCommentsListNewComments = commentsListNewComments.getEnrollmentId();
commentsListNewComments.setEnrollmentId(enrollment);
commentsListNewComments = em.merge(commentsListNewComments);
if (oldEnrollmentIdOfCommentsListNewComments != null && !oldEnrollmentIdOfCommentsListNewComments.equals(enrollment)) {
oldEnrollmentIdOfCommentsListNewComments.getCommentsList().remove(commentsListNewComments);
oldEnrollmentIdOfCommentsListNewComments = em.merge(oldEnrollmentIdOfCommentsListNewComments);
}
}
}