UserDao extends DerivableUriDao<User> { @Override public void persist(User entity) { if (entity.getClinic() != null && !organizationDao.exists(entity.getClinic().getUri())) { organizationDao.persist(entity.getClinic()); } super.persist(entity); } @Autowired  UserDao(OrganizationDao organizationDao); @Override void persist(User entity); @Override void update(User entity);  }
@Test public void persistGeneratesUri() { final User user = Generator.generateUser(); executeInTransaction(() -> dao.persist(user)); assertNotNull(user.getUri()); final User result = em.find(User.class, user.getUri()); assertNotNull(result); }
@Test public void persistPersistsOrganizationIfItDoesNotExistYet() { final User user = Generator.generateUser(); user.setClinic(Generator.generateOrganization()); executeInTransaction(() -> dao.persist(user)); final Organization result = em.find(Organization.class, user.getClinic().getUri()); assertNotNull(result); }