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:de.zib.gndms.infra.model.GridEntityModelHandler.java

/**
 * Merges a detached model into the persistent store
 *
 * @param emParam the EntityManager to be used or null for an EM from this handler's system
 * @param model//from  w w w  . j ava  2s.c  om
 */
final public @NotNull M mergeModel(final EntityManager emParam, final @NotNull M model) {
    return txRun(emParam, new Function<EntityManager, M>() {
        public M apply(@com.google.common.base.Nullable @NotNull EntityManager em) {
            em.merge(model);
            return model;
        }
    });
}

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

@Override
@PortalTransactional//w w  w.  j ava  2s  .c o  m
public void deletePortletEntity(IPortletEntity portletEntity) {
    Validate.notNull(portletEntity, "portletEntity can not be null");

    final IPortletEntity persistentPortletEntity;
    final EntityManager entityManager = this.getEntityManager();
    if (entityManager.contains(portletEntity)) {
        persistentPortletEntity = portletEntity;
    } else {
        persistentPortletEntity = entityManager.merge(portletEntity);
    }

    entityManager.remove(persistentPortletEntity);
}

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

/**
 * Default merge//from  ww w.  j a  va2  s  .  c  o  m
 * 
 * @param obj
 *            object to merge
 * @throws MergeException
 *             on errors (transaction is being rolled back)
 * @return managed object
 */
public T merge(T obj) throws MergeException {
    if (obj == null) {
        return null;
    }
    EntityManager em = PersistenceUtil.getEntityManager();
    EntityTransaction t = em.getTransaction();

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

From source file:com.epam.training.taranovski.web.project.repository.implementation.VacancyRepositoryImplementation.java

@Override
public boolean delete(Vacancy vacancy) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = true;
    Vacancy managedVacancy = null;//www .j av  a  2s .com
    try {
        em.getTransaction().begin();
        managedVacancy = em.merge(vacancy);
        em.remove(managedVacancy);
        em.getTransaction().commit();

        success = true;
    } catch (RuntimeException e) {
        Logger.getLogger(VacancyRepositoryImplementation.class.getName()).info(e);
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
            success = false;
        }
        em.close();
    }

    return success;
}

From source file:org.synyx.hades.dao.orm.GenericJpaDao.java

public void delete(final T entity) {

    EntityManager em = getEntityManager();
    em.remove(em.contains(entity) ? entity : em.merge(entity));
}

From source file:org.springframework.batch.item.database.JpaItemWriter.java

/**
 * Do perform the actual write operation. This can be overridden in a
 * subclass if necessary./*from  ww w.ja  v a 2s . c o m*/
 *
 * @param entityManager the EntityManager to use for the operation
 * @param items the list of items to use for the write
 */
protected void doWrite(EntityManager entityManager, List<? extends T> items) {

    if (logger.isDebugEnabled()) {
        logger.debug("Writing to JPA with " + items.size() + " items.");
    }

    if (!items.isEmpty()) {
        long mergeCount = 0;
        for (T item : items) {
            if (!entityManager.contains(item)) {
                entityManager.merge(item);
                mergeCount++;
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug(mergeCount + " entities merged.");
            logger.debug((items.size() - mergeCount) + " entities found in persistence context.");
        }
    }

}

From source file:com.epam.training.taranovski.web.project.repository.implementation.OfferBidRepositoryImplementation.java

@Override
public boolean update(OfferBid something) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = false;

    try {//from   w  ww. j  a  v a  2  s .c o m
        em.getTransaction().begin();
        em.merge(something);
        em.getTransaction().commit();
        success = true;
    } catch (RuntimeException e) {
        Logger.getLogger(OfferBidRepositoryImplementation.class.getName()).info(e);
        success = false;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return success;
}

From source file:org.apache.click.extras.jpa.JpaForm.java

/**
 * Save or update the object to the database and return true.
 * <p/>// w  w w  . j  a va 2  s .  c o  m
 * If no object is added to the form using <tt>setValueObject()</tt>
 * then this method will: <ul>
 * <li>create a new instance of the Class</li>
 * <li>copy the form's field values to the objects properties</li>
 * <li>save the new object to the database</li>
 * </ul>
 * <p/>
 * If an existing persistent object is added to the form using
 * <tt>setValueObject()</tt> then this method will: <ul>
 * <li>load the persistent object record from the database</li>
 * <li>copy the form's field values to the objects properties</li>
 * <li>update the object in the database</li>
 * </ul>
 *
 * @return true if the object was saved or false otherwise
 */
public boolean saveChanges() {
    Object valueObject = getValueObject();

    EntityManager entityManager = getEntityManager();
    entityManager.merge(valueObject);

    return true;
}

From source file:com.openmeap.model.ModelServiceImpl.java

@Override
public <E extends ModelEntity, T extends ModelEntity> List<T> getOrdered(E entity, String listMethod,
        Comparator<T> comparator) {
    EntityManager entityManager = getEntityManager();
    entityManager.getTransaction().begin();
    entityManager.merge(entity);
    List<T> ents;/* w  w  w .j  a va  2s. c  o  m*/
    try {
        ents = (List<T>) entity.getClass().getMethod(listMethod).invoke(entity);
    } catch (Exception e) {
        throw new PersistenceException(e);
    }
    Collections.sort(ents, comparator);
    entityManager.getTransaction().commit();
    return ents;
}

From source file:fredboat.db.entity.SearchResult.java

/**
 * Persist a search in the database./*from   w w w .  j  a v  a 2s.  c om*/
 *
 * @return the merged SearchResult object
 */
public SearchResult save() {
    DatabaseManager dbManager = FredBoat.getDbManager();
    if (dbManager == null || !dbManager.isAvailable()) {
        throw new DatabaseNotReadyException();
    }

    EntityManager em = dbManager.getEntityManager();
    try {
        em.getTransaction().begin();
        SearchResult managed = em.merge(this);
        em.getTransaction().commit();
        return managed;
    } catch (PersistenceException e) {
        log.error("Unexpected error while saving a search result for provider {} and search term {}",
                searchResultId.provider, searchResultId.searchTerm, e);
        throw new DatabaseNotReadyException(e);
    } finally {
        em.close();
    }
}