@Override
  public Set<Person> getSiblingsAll(Person thisPerson) {
    // return full and half siblings of thisPerson
    // exclude thisPerson from the set

    Set<Person> siblings = new HashSet<Person>();
    List<Person> parents = new ArrayList<Person>();

    em.getTransaction().begin();
    Query q =
        em.createQuery(
            "select b.parents from Birth b, IN(b.person) p WHERE p.id = :personId"
                + " or p.id = :parent2Id");
    q.setParameter("personId", thisPerson.getId());

    parents.addAll((Collection<? extends Person>) q.getResultList());

    if (!parents.isEmpty()) {
      if (parents.size() == 2) {
        siblings = getChildrenAll(parents.get(0), parents.get(1));
      } else if (parents.size() == 1) {
        siblings = getChildren(parents.get(0));
      }
    }
    em.getTransaction().commit();
    siblings.remove(thisPerson);
    return siblings;
  }
 @Override
 public Adoption findAdoptionRecord(Person person) {
   // Could be null
   em.getTransaction().begin();
   Query q = em.createQuery("select a from Adoption a WHERE a.person.id = :idParam");
   q.setParameter("idParam", person.getId());
   List<Adoption> results = (List<Adoption>) q.getResultList();
   em.getTransaction().commit();
   return (results != null && results.size() > 0) ? results.get(0) : null;
 }
 @SuppressWarnings("unchecked")
 @Override
 public Person getPerson(Long id) {
   em.getTransaction().begin();
   Query q = em.createQuery("select p from Person p where p.id = :idParam");
   q.setParameter("idParam", id);
   List<Person> people = (List<Person>) q.getResultList();
   em.getTransaction().commit();
   return (people != null) ? people.get(0) : null;
 }
  @Override
  public Death findDeathRecord(Person person) {
    // Could be null
    List<Death> myresults = new ArrayList<Death>();

    em.getTransaction().begin();
    TypedQuery<Death> q =
        em.createQuery("select b from Death b WHERE b.person.id = :idParam", Death.class);
    q.setParameter("idParam", person.getId());

    myresults.addAll(q.getResultList());
    em.getTransaction().commit();
    return myresults.size() >= 1 ? myresults.get(0) : null;
  }
  @Override
  public Person getSpouse(Person person) {
    List<Person> people = new ArrayList<Person>();
    em.getTransaction().begin();
    Query q =
        em.createQuery(
            "select m.spouse from Marriage m "
                + "WHERE m.person.id = :personId order by m.eventdate desc");
    q.setParameter("personId", person.getId());

    people.addAll((Collection<? extends Person>) q.getResultList());
    em.getTransaction().commit();
    return (!people.isEmpty() ? people.get(0) : null);
  }
  @Override
  public void addAdoptedParent(Person child, Person parent) {
    em.getTransaction().begin();
    TypedQuery<Adoption> q =
        em.createQuery("select b from Adoption b where b.person.id = :idParam", Adoption.class);
    q.setParameter("idParam", child.getId());

    List<Adoption> results = (List<Adoption>) q.getResultList();
    Adoption a = (results != null && results.size() > 0) ? results.get(0) : null;
    if (a != null) {
      a.addParent(parent);
      em.merge(a);
    }
    em.getTransaction().commit();
  }
  @Override
  public void addParent(Person child, Person parent) {
    em.getTransaction().begin();
    TypedQuery<Birth> q =
        em.createQuery("select b from Birth b where b.person.id = :idParam", Birth.class);
    q.setParameter("idParam", child.getId());

    List<Birth> results = (List<Birth>) q.getResultList();
    Birth b = (results != null && results.size() > 0) ? results.get(0) : null;
    if (b != null) {
      b.addParent(parent);
      em.merge(b);
    }
    em.getTransaction().commit();
  }