@Override public void delete(Long id) { // delete associated entries from TEAM and TEAM_PERSON tables, but not from the PERSON table Team team = get(id); if (team == null) return; for (Person p : team.getMembers()) { teamPersonDao.delete(new TeamPerson(team.getId(), p.getId())); } teamDao.delete(team.getId()); }
@Override public Team save(final Team team) { long id; if (team.getId() == null) { id = insertWithTxCallback(team); } else { id = update(team); } return get(id); }
public Team insertWithTxHandle(final Team team) { // in this case we use an explicit handle, and attach the dao's to the same handle (connection) try (Handle handle = jdbiHelper.getTxHandle()) { handle.begin(); TeamDao teamDao = handle.attach(TeamDao.class); TeamPersonDao teamPersonDao = handle.attach(TeamPersonDao.class); // team->person mapping table long teamId; if (team.getPointOfContact() != null) { teamId = teamDao.insertWithPoC(team); } else { teamId = teamDao.insertWithoutPoC(team); } for (Person p : team.getMembers()) { // update the team->person mapping table teamPersonDao.insert(new TeamPerson(teamId, p.getId())); } // add test code for checking that TX is handled correctly checkIfTxShouldBeRolledBack(team); handle.commit(); return get(teamId); } }
private void getTeamMembers(Team team, TeamPersonDao teamPersonDao, PersonDao personDao) { List<TeamPerson> teamPersonList = teamPersonDao.findByTeamId(team.getId()); for (TeamPerson tp : teamPersonList) { team.addMember(personDao.get(tp.getPersonId())); } }
static void checkIfTxShouldBeRolledBack(Team team) { if (team.getName().equals("FAIL")) { throw new IllegalStateException("Name of team was FAIL"); } }