@Test public void testUpdate() { final JPAEnvironment env = getEnvironment(); final EntityManager em = env.getEntityManager(); try { env.beginTransaction(em); final Course course = createAndPersistCourse(em); final Long courseId = Long.valueOf(course.getCourseId()); env.commitTransactionAndClear(em); env.beginTransaction(em); // the remove of Doris takes place on the managed entity em.find(Course.class, courseId).removeAttendee(em.find(Employee.class, EMP_ID_DORIS)); env.commitTransactionAndClear(em); final Course storedCourse = em.find(Course.class, courseId); verify(storedCourse != null, "didnt find course again"); verify(storedCourse.getAttendees() != null, "course lost attendees"); verify( storedCourse.getAttendees().size() == 1, "number of attendees in course (expected: 1, got: " + storedCourse.getAttendees().size() + ")."); for (final Employee attendee : storedCourse.getAttendees()) { verify("Sabine".equals(attendee.getFirstName()), "Wrong attendee: " + attendee); } } finally { closeEntityManager(em); } }
@Test public void testMergeChangedRelation() { final JPAEnvironment env = getEnvironment(); final EntityManager em = env.getEntityManager(); try { env.beginTransaction(em); final Course course = createAndPersistCourse(em); final Long courseId = Long.valueOf(course.getCourseId()); env.commitTransactionAndClear(em); env.beginTransaction(em); // the remove of Doris takes place on the detached entity: but rather than relying on // cascade, the attendees are retrieved from em again. course.clearAttendees(); course.addAttendee(em.find(Employee.class, EMP_ID_SABINE)); final List<Employee> attendeesBeforeMerge = course.getAttendees(); verify( attendeesBeforeMerge.size() == 1, "wrong number of attendees before merge(): " + attendeesBeforeMerge.size()); final List<Employee> attendeesAfterMerge = em.merge(course).getAttendees(); verify( attendeesAfterMerge.size() == 1, "wrong number of attendees after merge(): " + attendeesAfterMerge.size()); env.commitTransactionAndClear(em); final Course storedCourse = em.find(Course.class, courseId); verify(storedCourse != null, "didnt find course again"); verify(storedCourse.getAttendees() != null, "course lost attendees"); verify( storedCourse.getAttendees().size() == 1, "number of attendees in course (expected: 1, got: " + storedCourse.getAttendees().size() + ")."); for (final Employee attendee : storedCourse.getAttendees()) { verify("Sabine".equals(attendee.getFirstName()), "Wrong attendee: " + attendee); } } finally { closeEntityManager(em); } }