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.epam.training.taranovski.web.project.repository.implementation.UserSkillRepositoryImplementation.java

@Override
public boolean delete(UserSkill userSkill) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = true;
    UserSkill managedUserSkill = null;/*from  w w  w  .ja v a2s.c  o m*/
    try {
        em.getTransaction().begin();
        managedUserSkill = em.merge(userSkill);
        em.remove(managedUserSkill);
        em.getTransaction().commit();

        success = true;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
            success = false;
        }
        em.close();
    }

    return success;
}

From source file:org.apereo.portal.groups.pags.dao.jpa.JpaPersonAttributesGroupTestDefinitionDao.java

@PortalTransactional
@Override/*from w ww  . j a v  a  2s. c o m*/
public IPersonAttributesGroupTestDefinition updatePersonAttributesGroupTestDefinition(
        IPersonAttributesGroupTestDefinition personAttributesGroupTestDefinition) {
    Validate.notNull(personAttributesGroupTestDefinition,
            "personAttributesGroupTestDefinition can not be null");

    final IPersonAttributesGroupTestDefinition persistentDefinition;
    final EntityManager entityManager = this.getEntityManager();
    if (entityManager.contains(personAttributesGroupTestDefinition)) {
        persistentDefinition = personAttributesGroupTestDefinition;
    } else {
        persistentDefinition = entityManager.merge(personAttributesGroupTestDefinition);
    }

    this.getEntityManager().persist(persistentDefinition);
    return persistentDefinition;
}

From source file:org.sigmah.server.endpoint.export.sigmah.handler.ProjectReportModelHandler.java

/**
 * Save the section whith its sub sections and key questions
 * //w  ww  .jav  a2s.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.apereo.portal.i18n.dao.jpa.JpaMessageDao.java

@Override
@PortalTransactional/*from w  w w. java  2  s  . c om*/
public void deleteMessage(Message message) {
    Validate.notNull(message, "message can not be null");

    final Message msg;
    final EntityManager entityManager = this.getEntityManager();
    if (entityManager.contains(message)) {
        msg = message;
    } else {
        msg = entityManager.merge(message);
    }
    entityManager.remove(msg);
}

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

@Override
public boolean delete(VacancySkill vacancySkill) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = true;
    VacancySkill managedVacancySkill = null;
    try {/*from ww  w.  j a  va2  s  .c  o  m*/
        em.getTransaction().begin();
        managedVacancySkill = em.merge(vacancySkill);
        em.remove(managedVacancySkill);
        em.getTransaction().commit();

        success = true;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
            success = false;
        }
        em.close();
    }

    return success;
}

From source file:org.opencastproject.messages.MailService.java

public static EmailConfigurationDto mergeEmailConfiguration(EmailConfiguration config, String organization,
        EntityManager em) {
    Option<EmailConfigurationDto> configOption = findEmailConfiguration(organization, em);
    EmailConfigurationDto dto;//w ww.  ja  v a  2s .c  om
    if (configOption.isSome()) {
        dto = configOption.get();
        dto.setTransport(config.getTransport());
        dto.setPassword(config.getPassword());
        dto.setPort(config.getPort());
        dto.setServer(config.getServer());
        dto.setSsl(config.isSsl());
        dto.setUserName(config.getUserName());
        em.merge(dto);
    } else {
        dto = new EmailConfigurationDto(organization, config.getTransport(), config.getServer(),
                config.getPort(), config.getUserName(), config.getPassword(), config.isSsl());
        em.persist(dto);
    }
    return dto;
}

From source file:org.spc.ofp.tubs.domain.common.CommonRepository.java

public boolean saveImportStatus(final ImportStatus status) {
    boolean success = false;
    final EntityManager mgr = emf.createEntityManager();
    final EntityTransaction xa = mgr.getTransaction();
    xa.begin();/*from ww w.  java 2  s.c  o  m*/
    try {
        if (status.getId() > 0L) {
            mgr.merge(status);
        } else {
            mgr.persist(status);
        }
        xa.commit();
        success = true;
    } catch (Exception ignoreMe) {
        rollbackQuietly(xa);
    } finally {
        mgr.close();
    }
    return success;
}

From source file:org.apereo.portal.layout.dlm.FragmentDefinitionDao.java

@Override
@PortalTransactional//www.  j ava2  s.  c  om
public void removeFragmentDefinition(FragmentDefinition fd) {

    Validate.notNull(fd, "FragmentDefinition can not be null");
    final FragmentDefinition persistentFragmentDefinition;
    final EntityManager entityManager = this.getEntityManager();
    if (entityManager.contains(fd)) {
        persistentFragmentDefinition = fd;
    } else {
        persistentFragmentDefinition = entityManager.merge(fd);
    }

    entityManager.remove(persistentFragmentDefinition);
}

From source file:org.apereo.portal.groups.pags.dao.jpa.JpaPersonAttributesGroupTestGroupDefinitionDao.java

@PortalTransactional
@Override/*from   ww  w  .j  a va 2 s  . c  o  m*/
public IPersonAttributesGroupTestGroupDefinition updatePersonAttributesGroupTestGroupDefinition(
        IPersonAttributesGroupTestGroupDefinition personAttributesGroupTestGroupDefinition) {
    Validate.notNull(personAttributesGroupTestGroupDefinition,
            "personAttributesGroupTestGroupDefinition can not be null");

    final IPersonAttributesGroupTestGroupDefinition persistentDefinition;
    final EntityManager entityManager = this.getEntityManager();
    if (entityManager.contains(personAttributesGroupTestGroupDefinition)) {
        persistentDefinition = personAttributesGroupTestGroupDefinition;
    } else {
        persistentDefinition = entityManager.merge(personAttributesGroupTestGroupDefinition);
    }

    this.getEntityManager().persist(persistentDefinition);
    return persistentDefinition;
}

From source file:op.care.med.inventory.DlgOpenStock.java

private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
    if (cmbBestID.getSelectedIndex() > 0) {
        EntityManager em = OPDE.createEM();
        try {//from  w  ww  .j ava2s.  c  o  m
            em.getTransaction().begin();
            medStock = em.merge((MedStock) cmbBestID.getSelectedItem());
            em.lock(medStock, LockModeType.OPTIMISTIC);
            em.lock(em.merge(medStock.getInventory().getResident()), LockModeType.OPTIMISTIC);

            medStock.setOpened(new Date());

            em.getTransaction().commit();
        } catch (javax.persistence.OptimisticLockException ole) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            if (ole.getMessage().indexOf("Class> entity.info.Resident") > -1) {
                OPDE.getMainframe().emptyFrame();
                OPDE.getMainframe().afterLogin();
            }
            OPDE.getDisplayManager().addSubMessage(DisplayManager.getLockMessage());
        } catch (Exception e) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            OPDE.fatal(e);
        } finally {
            em.close();
        }
        dispose();
    }
}