@Override public void setRoleIds(List<Long> newRoleIds) { if (newRoleIds == null) { newRoleIds = new LinkedList<Long>(); } final Set<Long> newRoleIdsSet = new LinkedHashSet<Long>(newRoleIds); final Map<Long, PersonRoleLink> newPersonRoleLinks = new LinkedHashMap<Long, PersonRoleLink>(); final EntityManager em = EntityManagerProvider.get(); for (final Entry<Long, PersonRoleLink> entry : personRoleLinks.entrySet()) { if (!newRoleIdsSet.contains(entry.getKey())) { em.remove(entry.getValue()); } } for (final Long newId : newRoleIds) { PersonRoleLink personRoleLink = personRoleLinks.get(newId); if (personRoleLink == null) { personRoleLink = new PersonRoleLink(); if (getId() == null) { em.persist(this); } personRoleLink.setPersonId(getId()); personRoleLink.setRoleId(newId); em.persist(personRoleLink); } newPersonRoleLinks.put(newId, personRoleLink); } personRoleLinks = newPersonRoleLinks; }
public Country getCountry() { if (country == null && countryId != null) { country = EntityManagerProvider.get().find(Country.class, countryId); } return country; }