StudyDao extends BaseDao<Study> { @Override public void persist(Study entity) { persistUsersIfNecessary(entity.getAdministrators()); persistUsersIfNecessary(entity.getParticipants()); assert entity.getAuthor() != null; super.persist(entity); } @Autowired  StudyDao(UserDao userDao); @Override void persist(Study entity); @Override void update(Study entity);  }
@Test public void persistPersistsUsersWhenTheyDoNotExist() { final Study study = generateStudyWithUsers(); executeInTransaction(() -> studyDao.persist(study)); final List<User> expectedUsers = new ArrayList<>(); expectedUsers.addAll(study.getAdministrators()); expectedUsers.addAll(study.getParticipants()); for (User expected : expectedUsers) { assertNotNull(em.find(User.class, expected.getUri())); } final Study result = studyDao.find(study.getUri()); verifyStudyUsers(study, result); }
@Test public void generatesKeyOnPersist() { final Study study = Generator.generateStudy(true); study.setAuthor(author); assertNull(study.getKey()); executeInTransaction(() -> studyDao.persist(study)); assertNotNull(study.getKey()); final Study result = studyDao.findByKey(study.getKey()); assertNotNull(result); assertEquals(study.getUri(), result.getUri()); }