EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Courses persistentCourses = em.find(Courses.class, courses.getCourseId());
User createrUserIdOld = persistentCourses.getCreaterUserId();
User createrUserIdNew = courses.getCreaterUserId();
List<Lecture> lectureListOld = persistentCourses.getLectureList();
List<Lecture> lectureListNew = courses.getLectureList();
List<Enrollment> enrollmentListOld = persistentCourses.getEnrollmentList();
List<Enrollment> enrollmentListNew = courses.getEnrollmentList();
List<String> illegalOrphanMessages = null;
for (Enrollment enrollmentListOldEnrollment : enrollmentListOld) {
if (!enrollmentListNew.contains(enrollmentListOldEnrollment)) {
if (illegalOrphanMessages == null) {
illegalOrphanMessages = new ArrayList<String>();
}
illegalOrphanMessages.add("You must retain Enrollment " + enrollmentListOldEnrollment + " since its courseId field is not nullable.");
}
}
if (illegalOrphanMessages != null) {
throw new IllegalOrphanException(illegalOrphanMessages);
}
if (createrUserIdNew != null) {
createrUserIdNew = em.getReference(createrUserIdNew.getClass(), createrUserIdNew.getId());
courses.setCreaterUserId(createrUserIdNew);
}
List<Lecture> attachedLectureListNew = new ArrayList<Lecture>();
for (Lecture lectureListNewLectureToAttach : lectureListNew) {
lectureListNewLectureToAttach = em.getReference(lectureListNewLectureToAttach.getClass(), lectureListNewLectureToAttach.getId());
attachedLectureListNew.add(lectureListNewLectureToAttach);
}
lectureListNew = attachedLectureListNew;
courses.setLectureList(lectureListNew);
List<Enrollment> attachedEnrollmentListNew = new ArrayList<Enrollment>();
for (Enrollment enrollmentListNewEnrollmentToAttach : enrollmentListNew) {
enrollmentListNewEnrollmentToAttach = em.getReference(enrollmentListNewEnrollmentToAttach.getClass(), enrollmentListNewEnrollmentToAttach.getId());
attachedEnrollmentListNew.add(enrollmentListNewEnrollmentToAttach);
}
enrollmentListNew = attachedEnrollmentListNew;
courses.setEnrollmentList(enrollmentListNew);
courses = em.merge(courses);
if (createrUserIdOld != null && !createrUserIdOld.equals(createrUserIdNew)) {
createrUserIdOld.getCoursesList().remove(courses);
createrUserIdOld = em.merge(createrUserIdOld);
}
if (createrUserIdNew != null && !createrUserIdNew.equals(createrUserIdOld)) {
createrUserIdNew.getCoursesList().add(courses);
createrUserIdNew = em.merge(createrUserIdNew);
}
for (Lecture lectureListOldLecture : lectureListOld) {
if (!lectureListNew.contains(lectureListOldLecture)) {
lectureListOldLecture.setCourseId(null);