Example usage for javax.persistence EntityManager persist

List of usage examples for javax.persistence EntityManager persist

Introduction

In this page you can find the example usage for javax.persistence EntityManager persist.

Prototype

public void persist(Object entity);

Source Link

Document

Make an instance managed and persistent.

Usage

From source file:org.sigmah.server.servlet.exporter.models.ProjectReportModelHandler.java

/**
 * Save the section whith its sub sections and key questions
 * // w w w .  ja  va  2 s  .  co m
 * @param section
 *          the section to save
 * @param subSections
 *          the subsections to save.
 * @param keyQuestions
 *          the key question to save.
 * @param em
 *          the entity manager
 */
private static void saveSectionSubSectionKeyQuestions(ProjectReportModelSection section,
        List<ProjectReportModelSection> subSections, List<KeyQuestion> keyQuestions, EntityManager em) {
    if (keyQuestions != null) {
        saveSectionKeyQuestion(section, keyQuestions, em);
    }
    if (subSections != null) {
        for (ProjectReportModelSection subSection : subSections) {
            subSection.setParentSectionModelId(section.getId());
            List<ProjectReportModelSection> subSubSections = subSection.getSubSections();
            List<KeyQuestion> questions = subSection.getKeyQuestions();
            if (subSubSections != null || keyQuestions != null) {
                // Save sub section before its sub sections and its key questions
                subSection.setSubSections(null);
                subSection.setKeyQuestions(null);
                em.persist(subSection);
                // Save the sub sections and the key questions of the subsection
                saveSectionSubSectionKeyQuestions(subSection, subSubSections, questions, em);
                subSection.setSubSections(subSubSections);
                if (subSection != null) {
                    em.merge(subSection);
                }
            } else {
                if (subSection != null) {
                    em.persist(subSection);
                }
            }
        }
    }

}

From source file:org.apache.airavata.registry.core.experiment.catalog.ExpCatResourceUtils.java

/**
 * @param gatewayResource//from w ww .j  a v a 2  s  .  c  o  m
 * @param userResource
 */
public static WorkerResource addGatewayWorker(GatewayResource gatewayResource, UserResource userResource)
        throws RegistryException {
    EntityManager em = null;
    try {
        em = getEntityManager();
        em.getTransaction().begin();
        if (!isGatewayExist(gatewayResource.getGatewayName())) {
            gatewayResource.save();
        }
        if (!isUserExist(userResource.getUserName(), gatewayResource.getGatewayId())) {
            userResource.save();
        }
        Gateway gateway = em.find(Gateway.class, gatewayResource.getGatewayId());
        GatewayWorker gatewayWorker = new GatewayWorker();
        gatewayWorker.setGateway(gateway);
        gatewayWorker.setUserName(userResource.getUserName());
        em.persist(gatewayWorker);
        em.getTransaction().commit();
        em.close();
        return (WorkerResource) Utils.getResource(ResourceType.GATEWAY_WORKER, gatewayWorker);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RegistryException(e);
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }
}

From source file:com.gerenciaProyecto.DaoImple.VentaDaoImpl.java

@Override
@Transactional//w  w w .j a  v a2s .co m
public void AgregarVenta(Venta p_Venta) {
    EntityManager em = getEntityManager();
    try {
        em.persist(p_Venta);
        em.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:uk.co.threeonefour.ifictionary.web.user.dao.JpaUserDao.java

@Transactional
@Override
public User add(User user) {

    EntityManager em = entityManager;
    em.persist(user);
    em.flush();
    return user;
}

From source file:springchat.service.MessagesServiceImpl.java

@Override
@Transactional//from www  .j  ava2s . co m
public MessageEntity save(long date, String user, String message) {
    final MessageEntity bean = new MessageEntity();
    bean.setDate(new Date(date));
    bean.setUser(user);
    bean.setMessage(message);
    eao.execute(new BaseEAO.Executable<Void>() {
        @Override
        public Void execute(EntityManager em) {
            em.persist(bean);
            return null;
        }
    });
    return bean;
}

From source file:dao.UsuarioDao.java

public void cadastrarPessoa(Usuario usuario) {
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("LoginUsersPU");
    EntityManager em = factory.createEntityManager();

    em.getTransaction().begin();//w w w .j a va  2  s .co m
    em.persist(usuario);
    em.getTransaction().commit();
    em.close();
}

From source file:cn.buk.hotel.dao.CityDaoImpl.java

@Override
@Transactional/*from w ww  .  j  a v a2s.  com*/
public int create(City city) {
    int retStatus = 0;
    EntityManager em = getEm();
    try {
        em.persist(city);
        retStatus = 1;
    } catch (Exception ex) {
        retStatus = -1;
        logger.error(ex.getMessage());
    }
    return retStatus;
}

From source file:com.enioka.jqm.tools.Helpers.java

/**
 * Checks if a parameter exists. If it exists, it is left untouched. If it doesn't, it is created. Only works for parameters which key
 * is unique. Must be called from within an open JPA transaction.
 *//*from www  .j  a  v  a 2s  .  co m*/
static void initSingleParam(String key, String initValue, EntityManager em) {
    try {
        em.createQuery("SELECT n from GlobalParameter n WHERE n.key = :key", GlobalParameter.class)
                .setParameter("key", key).getSingleResult();
        return;
    } catch (NoResultException e) {
        GlobalParameter gp = new GlobalParameter();
        gp.setKey(key);
        gp.setValue(initValue);
        em.persist(gp);
    } catch (NonUniqueResultException e) {
        // It exists! Nothing to do...
    }
}

From source file:com.gerenciaProyecto.DaoImple.UsuarioDaoImpl.java

@Override
@Transactional//w w w .  j a v a  2  s.  co  m
public void crear(Usuario usuario) {
    EntityManager em = getEntityManager();
    try {
        em.persist(usuario);
        em.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.nuxeo.theme.webwidgets.providers.PersistentProviderPerUser.java

@Override
public synchronized Widget createWidget(String widgetTypeName) throws ProviderException {
    if (widgetTypeName == null) {
        throw new ProviderException("Widget type name is undefined");
    }/*from  w ww.  jav  a 2  s  .co m*/
    Principal currentNuxeoPrincipal = getCurrentPrincipal();
    if (currentNuxeoPrincipal == null) {
        log.warn("Could not get the current user from the context.");
        return null;
    }

    final WidgetEntity widget = new WidgetEntity(widgetTypeName);
    widget.setScope(currentNuxeoPrincipal.getName());
    try {
        getPersistenceProvider().run(true, new RunVoid() {
            public void runWith(EntityManager em) {
                em.persist(widget);
            }
        });
    } catch (ClientException e) {
        throw new ClientRuntimeException(e);
    }
    return widget;
}