Source:
public void testCourseComponentSession() { SimpleDateFormat format = new SimpleDateFormat("EEEE dd MMMM yyyy, HH:mm:ss"); Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 10); Date start = cal.getTime(); cal.add(Calendar.HOUR_OF_DAY, 2); Date end = cal.getTime(); CourseComponentSessionDAO ss1 = new CourseComponentSessionDAO("1", start, format.format(start), end, format.format(end), "location 1"); CourseComponentSessionDAO ss2 = new CourseComponentSessionDAO("2", start, format.format(start), end, format.format(end), "location 2"); CourseComponentSessionDAO ss3 = new CourseComponentSessionDAO("3", start, format.format(start), end, format.format(end), "location 3"); CourseComponentDAO courseComponent = courseDao.newCourseComponent("test"); courseComponent.setSource("source"); courseComponent.getComponentSessions().add(ss1); courseDao.save(courseComponent); sessionFactory.getCurrentSession().flush(); sessionFactory.getCurrentSession().clear(); courseComponent = courseDao.findCourseComponent("test"); assertEquals(1, courseComponent.getComponentSessions().size()); courseComponent.getComponentSessions().add(ss2); courseDao.save(courseComponent); sessionFactory.getCurrentSession().flush(); sessionFactory.getCurrentSession().clear(); courseComponent = courseDao.findCourseComponent("test"); assertEquals(2, courseComponent.getComponentSessions().size()); courseComponent.getComponentSessions().clear(); courseComponent.getComponentSessions().add(ss3); courseDao.save(courseComponent); sessionFactory.getCurrentSession().flush(); sessionFactory.getCurrentSession().clear(); courseComponent = courseDao.findCourseComponent("test"); assertEquals(1, courseComponent.getComponentSessions().size()); }

Target:
public void testCourseComponentSession() { SimpleDateFormat format = new SimpleDateFormat("EEEE dd MMMM yyyy, HH:mm:ss"); Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 10); Date start = cal.getTime(); cal.add(Calendar.HOUR_OF_DAY, 2); Date end = cal.getTime(); CourseComponentSessionDAO ss1 = new CourseComponentSessionDAO("1", start, format.format(start), end, format.format(end), "location 1"); CourseComponentSessionDAO ss2 = new CourseComponentSessionDAO("2", start, format.format(start), end, format.format(end), "location 2"); CourseComponentSessionDAO ss3 = new CourseComponentSessionDAO("3", start, format.format(start), end, format.format(end), "location 3"); CourseComponentDAO courseComponent = courseDao.newCourseComponent("test"); courseComponent.setSource("source"); courseComponent.getComponentSessions().add(ss1); courseDao.save(courseComponent); sessionFactory.getCurrentSession().flush(); sessionFactory.getCurrentSession().clear(); courseComponent = courseDao.findCourseComponent("test"); Iterator<CourseComponentSessionDAO> sessionsIt = courseComponent.getComponentSessions().iterator(); assertTrue("Expected at least one session", sessionsIt.hasNext()); assertEquals("location 1", sessionsIt.next().getLocation()); assertFalse("Should be no more than one session", sessionsIt.hasNext()); courseComponent.getComponentSessions().add(ss2); courseDao.save(courseComponent); sessionFactory.getCurrentSession().flush(); sessionFactory.getCurrentSession().clear(); courseComponent = courseDao.findCourseComponent("test"); assertEquals(2, courseComponent.getComponentSessions().size()); courseComponent.getComponentSessions().clear(); courseComponent.getComponentSessions().add(ss3); courseDao.save(courseComponent); sessionFactory.getCurrentSession().flush(); sessionFactory.getCurrentSession().clear(); courseComponent = courseDao.findCourseComponent("test"); assertEquals(1, courseComponent.getComponentSessions().size()); }