public void create(PypAdmAgend pypAdmAgend) {
        if (pypAdmAgend.getPypAdmAsistConList() == null) {
            pypAdmAgend.setPypAdmAsistConList(new ArrayList<PypAdmAsistCon>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            InfoPaciente idPaciente = pypAdmAgend.getIdPaciente();
            if (idPaciente != null) {
                idPaciente = em.getReference(idPaciente.getClass(), idPaciente.getId());
                pypAdmAgend.setIdPaciente(idPaciente);
            }
            PypAdmProgramas idPrograma = pypAdmAgend.getIdPrograma();
            if (idPrograma != null) {
                idPrograma = em.getReference(idPrograma.getClass(), idPrograma.getId());
                pypAdmAgend.setIdPrograma(idPrograma);
            }
            List<PypAdmAsistCon> attachedPypAdmAsistConList = new ArrayList<PypAdmAsistCon>();
            for (PypAdmAsistCon pypAdmAsistConListPypAdmAsistConToAttach : pypAdmAgend.getPypAdmAsistConList()) {
                pypAdmAsistConListPypAdmAsistConToAttach = em.getReference(pypAdmAsistConListPypAdmAsistConToAttach.getClass(), pypAdmAsistConListPypAdmAsistConToAttach.getId());
                attachedPypAdmAsistConList.add(pypAdmAsistConListPypAdmAsistConToAttach);
            }
            pypAdmAgend.setPypAdmAsistConList(attachedPypAdmAsistConList);
            em.persist(pypAdmAgend);
            if (idPaciente != null) {
                idPaciente.getPypAdmAgendList().add(pypAdmAgend);
                idPaciente = em.merge(idPaciente);
            }
            if (idPrograma != null) {
                idPrograma.getPypAdmAgendList().add(pypAdmAgend);
                idPrograma = em.merge(idPrograma);
            }
            for (PypAdmAsistCon pypAdmAsistConListPypAdmAsistCon : pypAdmAgend.getPypAdmAsistConList()) {
                PypAdmAgend oldIdAgendOfPypAdmAsistConListPypAdmAsistCon = pypAdmAsistConListPypAdmAsistCon.getIdAgend();
                pypAdmAsistConListPypAdmAsistCon.setIdAgend(pypAdmAgend);
                pypAdmAsistConListPypAdmAsistCon = em.merge(pypAdmAsistConListPypAdmAsistCon);
                if (oldIdAgendOfPypAdmAsistConListPypAdmAsistCon != null) {
                    oldIdAgendOfPypAdmAsistConListPypAdmAsistCon.getPypAdmAsistConList().remove(pypAdmAsistConListPypAdmAsistCon);
                    oldIdAgendOfPypAdmAsistConListPypAdmAsistCon = em.merge(oldIdAgendOfPypAdmAsistConListPypAdmAsistCon);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }