public void create(Long altId, Vote vote) throws RollbackFailureException, Exception {
    vote.setId(null);

    Alternative alternative = em.find(Alternative.class, altId);
    if (alternative == null) {
      throw new NonExistingEntityException("Alternative with Id = " + altId + " does not exist");
    }
    vote.setAlternative(alternative);

    em.persist(vote);
    if (alternative != null) {
      alternative.getVotes().add(vote);
      alternative = em.merge(alternative);
    }
  }
 public void destroy(Long id)
     throws NonExistingEntityException, RollbackFailureException, Exception {
   Vote vote;
   try {
     vote = em.getReference(Vote.class, id);
     vote.getId();
   } catch (EntityNotFoundException enfe) {
     throw new NonExistingEntityException("The vote with id " + id + " no longer exists.", enfe);
   }
   Alternative alternative = vote.getAlternative();
   if (alternative != null) {
     alternative.getVotes().remove(vote);
     alternative = em.merge(alternative);
   }
   em.remove(vote);
 }
  public void edit(Long altId, Vote vote)
      throws NonExistingEntityException, RollbackFailureException, Exception {
    Alternative alternative = em.find(Alternative.class, altId);
    if (alternative == null) {
      throw new NonExistingEntityException("Alternative with Id = " + altId + " does not exist");
    }
    vote.setAlternative(alternative);

    Vote persistentVote = em.find(Vote.class, vote.getId());
    Alternative alternativeOld = persistentVote.getAlternative();
    Alternative alternativeNew = vote.getAlternative();
    if (alternativeNew != null) {
      alternativeNew = em.getReference(alternativeNew.getClass(), alternativeNew.getId());
      vote.setAlternative(alternativeNew);
    }
    vote = em.merge(vote);
    if (alternativeOld != null && !alternativeOld.equals(alternativeNew)) {
      alternativeOld.getVotes().remove(vote);
      alternativeOld = em.merge(alternativeOld);
    }
    if (alternativeNew != null && !alternativeNew.equals(alternativeOld)) {
      alternativeNew.getVotes().add(vote);
      alternativeNew = em.merge(alternativeNew);
    }
  }