Example usage for javax.persistence EntityManager merge

List of usage examples for javax.persistence EntityManager merge

Introduction

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

Prototype

public <T> T merge(T entity);

Source Link

Document

Merge the state of the given entity into the current persistence context.

Usage

From source file:com.yahoo.sql4d.indexeragent.meta.DBHandler.java

private void addUpdateDeleteEntity(Object entity, Action action) {
    EntityManager em = getEntityManager();
    try {/*from  ww w .  ja v  a 2  s  . c  o  m*/
        em.getTransaction().begin();
        switch (action) {
        case ADD:
            em.persist(entity);
            break;
        case UPDATE:
            em.merge(entity);
            break;
        case DELETE:
            em.remove(entity);
            break;
        }
    } catch (RuntimeException e) {
        log.error("Something wrong persisting/merging/removing entity {}, so rolling back . Exception is {}",
                entity, ExceptionUtils.getStackTrace(e));
        em.getTransaction().rollback();
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().commit();
        }
        em.close();
    }
}

From source file:de.iai.ilcd.model.dao.AbstractDao.java

/**
 * Default remove: bring back to persistence context if required and delete
 * //from   w ww .ja  v  a  2s. c o m
 * @param obj
 *            object to remove
 * @return remove object
 * @throws Exception
 *             on errors (transaction is being rolled back)
 */
public T remove(T obj) throws Exception {
    if (obj == null) {
        return null;
    }
    EntityManager em = PersistenceUtil.getEntityManager();
    EntityTransaction t = em.getTransaction();

    try {
        t.begin();
        T tmp = em.contains(obj) ? obj : em.merge(obj);
        em.remove(tmp);
        t.commit();
        return tmp;
    } catch (Exception e) {
        t.rollback();
        throw e;
    }
}

From source file:op.care.med.structure.DlgTradeForm.java

private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
    EntityManager em = OPDE.createEM();
    try {//www.java  2s.c o m
        em.getTransaction().begin();
        TradeForm myTradeForm = em.merge(tradeForm);
        em.lock(myTradeForm, LockModeType.OPTIMISTIC);
        myTradeForm.setSubtext(txtZusatz.getText());
        DosageForm dosageForm = em.merge((DosageForm) cmbForm.getSelectedItem());
        em.lock(dosageForm, LockModeType.OPTIMISTIC);
        myTradeForm.setDosageForm(dosageForm);

        em.getTransaction().commit();

        tradeForm = myTradeForm;
    } catch (Exception e) {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        OPDE.fatal(e);
    } finally {
        em.close();
    }
    dispose();
}

From source file:org.apereo.portal.portlet.dao.jpa.JpaPortletDefinitionDao.java

@Override
@PortalTransactional//from  ww w . j a v a2s. c  om
public void deletePortletDefinition(IPortletDefinition definition) {
    Validate.notNull(definition, "definition can not be null");

    final IPortletDefinition persistentPortletDefinition;
    final EntityManager entityManager = this.getEntityManager();
    if (entityManager.contains(definition)) {
        persistentPortletDefinition = definition;
    } else {
        persistentPortletDefinition = entityManager.merge(definition);
    }

    entityManager.remove(persistentPortletDefinition);
}

From source file:org.apereo.portal.persondir.dao.jpa.JpaLocalAccountDaoImpl.java

@Override
@PortalTransactional//www .ja  v  a2  s.  c o  m
public void deleteAccount(ILocalAccountPerson account) {
    Validate.notNull(account, "definition can not be null");

    final EntityManager entityManager = this.getEntityManager();

    final ILocalAccountPerson persistentAccount;
    if (entityManager.contains(account)) {
        persistentAccount = account;
    } else {
        persistentAccount = entityManager.merge(account);
    }

    entityManager.remove(persistentAccount);
}

From source file:de.iai.ilcd.model.dao.AbstractDao.java

/**
 * Default merge/*  w  w  w  .ja va 2s . com*/
 * 
 * @param objs
 *            objects to merge
 * @return list of merged/managed objects
 * @throws MergeException
 *             on errors (transaction is being rolled back)
 */
public Collection<T> merge(Collection<T> objs) throws MergeException {
    if (objs == null || objs.isEmpty()) {
        return null;
    }
    final Collection<T> tmp = new ArrayList<T>();
    EntityManager em = PersistenceUtil.getEntityManager();
    EntityTransaction t = em.getTransaction();

    try {
        t.begin();
        for (T obj : objs) {
            tmp.add(em.merge(obj));
        }
        t.commit();
        return tmp;
    } catch (Exception e) {
        t.rollback();
        throw new MergeException("Cannot merge changes to " + String.valueOf(objs) + " into the database", e);
    }
}

From source file:org.jasig.portal.portlet.dao.jpa.JpaMarketplaceRatingDao.java

/**
 * @param entity to delete - can not be null
 *///from w w  w.  j av a  2 s.co m
@Override
@PortalTransactional
public void deleteRating(IMarketplaceRating marketplaceRatingImplementation) {
    Validate.notNull(marketplaceRatingImplementation, "ratingPK can not be null");
    final IMarketplaceRating persistantMarketplaceRatingImpl;
    final EntityManager entityManager = this.getEntityManager();
    if (entityManager.contains(marketplaceRatingImplementation)) {
        persistantMarketplaceRatingImpl = marketplaceRatingImplementation;
    } else {
        persistantMarketplaceRatingImpl = entityManager.merge(marketplaceRatingImplementation);
    }
    entityManager.remove(persistantMarketplaceRatingImpl);
}

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

public void setWidgetState(final Widget widget, final WidgetState state) throws ProviderException {
    if (widget == null) {
        throw new ProviderException("Widget is undefined");
    }//from  w w  w .  j  av a  2s. c  o m
    if (state == null) {
        throw new ProviderException("Widget state is undefined");
    }

    try {
        getPersistenceProvider().run(true, new RunVoid() {
            public void runWith(EntityManager em) {
                ((WidgetEntity) widget).setState(state);
                em.merge(widget);
            }
        });
    } catch (ClientException e) {
        throw new ProviderException(e);
    }

}

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

public void addWidget(final Widget widget, final String regionName, final int order) throws ProviderException {
    if (widget == null) {
        throw new ProviderException("Widget is undefined");
    }//from w  ww  .  j a  v a  2s .com
    if (regionName == null) {
        throw new ProviderException("Region name is undefined");
    }

    List<Widget> widgets = getWidgets(regionName);
    widgets.add(order, widget);
    reorderWidgets(widgets);

    try {
        getPersistenceProvider().run(true, new RunVoid() {
            public void runWith(EntityManager em) {
                ((WidgetEntity) widget).setRegion(regionName);
                em.merge(widget);
            }
        });
    } catch (ClientException e) {
        throw new ProviderException(e);
    }
}

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

public void setWidgetPreferences(final Widget widget, final Map<String, String> preferences)
        throws ProviderException {
    if (widget == null) {
        throw new ProviderException("Widget is undefined");
    }/*from   w w  w  . ja va  2  s.  c o m*/
    if (preferences == null) {
        throw new ProviderException("Widget preferences are undefined");
    }

    try {
        getPersistenceProvider().run(true, new RunVoid() {
            public void runWith(EntityManager em) {
                ((WidgetEntity) widget).setPreferences(preferences);
                em.merge(widget);
            }
        });
    } catch (ClientException e) {
        throw new ProviderException(e);
    }

}