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.romb.hashfon.helper.Helper.java

public void mergeObject(EntityManager em, Object o) {
    em.getTransaction().begin();
    em.merge(o);
    em.getTransaction().commit();
}

From source file:vn.edu.vnuk.tasks_jpa.dao.TaskDao.java

public void update(Task task) throws SQLException {

    EntityManager manager = getEntityManager();
    manager.getTransaction().begin();/*  w ww .  jav  a  2 s .c om*/
    manager.merge(task);
    manager.getTransaction().commit();
    manager.close();

}

From source file:vn.edu.vnuk.tasks_jpa.dao.TaskDao.java

public void delete(Long id) throws SQLException {

    Task task = this.read(id);

    EntityManager manager = getEntityManager();
    manager.getTransaction().begin();//from   ww  w .j av a2 s  . com
    task = manager.merge(task);
    manager.remove(task);
    manager.getTransaction().commit();
    manager.close();

}

From source file:cz.fi.muni.pa165.daoImpl.TroopDAOImpl.java

@Override
public void updateTroop(Troop troop) throws IllegalArgumentException {
    if (troop == null || troop.getName() == null || troop.getMission() == null
            || troop.getAmountOfMoney() == null) {
        throw new IllegalArgumentException("Update troop called with wrong param");
    }//from   w w w  .  jav a  2 s  .c o  m
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    em.merge(troop);
    em.getTransaction().commit();
    em.close();
}

From source file:entity.files.SYSFilesTools.java

public static List<SYSFiles> putFiles(File[] files, Object attachable) {
    ArrayList<SYSFiles> successful = new ArrayList<SYSFiles>(files.length);
    FileTransferClient ftp = getFTPClient();

    if (ftp != null) {
        EntityManager em = OPDE.createEM();
        try {//from  www  . j  av a2  s . c  o m
            em.getTransaction().begin();

            for (File file : files) {
                if (file.isFile()) { // prevents exceptions if somebody has the bright idea to include directories.
                    SYSFiles sysfile = putFile(em, ftp, file);
                    if (attachable != null) {
                        if (attachable instanceof NReport) {
                            SYSNR2FILE link = em.merge(new SYSNR2FILE(sysfile, (NReport) attachable,
                                    OPDE.getLogin().getUser(), new Date()));
                            sysfile.getNrAssignCollection().add(link);
                            ((NReport) attachable).getAttachedFilesConnections().add(link);
                        } else if (attachable instanceof Prescription) {
                            SYSPRE2FILE link = em.merge(new SYSPRE2FILE(sysfile, (Prescription) attachable,
                                    OPDE.getLogin().getUser(), new Date()));
                            sysfile.getPreAssignCollection().add(link);
                            ((Prescription) attachable).getAttachedFilesConnections().add(link);
                        } else if (attachable instanceof ResInfo) {
                            SYSINF2FILE link = em.merge(new SYSINF2FILE(sysfile, (ResInfo) attachable,
                                    OPDE.getLogin().getUser(), new Date()));
                            sysfile.getBwiAssignCollection().add(link);
                            ((ResInfo) attachable).getAttachedFilesConnections().add(link);
                        } else if (attachable instanceof ResValue) {
                            SYSVAL2FILE link = em.merge(new SYSVAL2FILE(sysfile, (ResValue) attachable,
                                    OPDE.getLogin().getUser(), new Date()));
                            sysfile.getValAssignCollection().add(link);
                            ((ResValue) attachable).getAttachedFilesConnections().add(link);
                        } else if (attachable instanceof NursingProcess) {
                            SYSNP2FILE link = em.merge(new SYSNP2FILE(sysfile, (NursingProcess) attachable,
                                    OPDE.getLogin().getUser(), new Date()));
                            sysfile.getNpAssignCollection().add(link);
                            ((NursingProcess) attachable).getAttachedFilesConnections().add(link);
                        } else if (attachable instanceof Training) {
                            Training2File link = em.merge(new Training2File(sysfile, (Training) attachable,
                                    OPDE.getLogin().getUser(), new Date()));
                            sysfile.getTrAssignCollection().add(link);
                            ((Training) attachable).getAttachedFilesConnections().add(link);
                        } else if (attachable instanceof Users) {
                            User2File link = em.merge(new User2File(sysfile, (Users) attachable,
                                    OPDE.getLogin().getUser(), new Date()));
                            sysfile.getUsersAssignCollection().add(link);
                            ((Users) attachable).getAttachedFilesConnections().add(link);
                        } else if (attachable instanceof Resident) {
                            Resident2File link = em.merge(new Resident2File(sysfile, (Resident) attachable,
                                    OPDE.getLogin().getUser(), new Date()));
                            sysfile.getResidentAssignCollection().add(link);
                            ((Resident) attachable).getAttachedFilesConnections().add(link);
                        } else if (attachable instanceof Qmsplan) {
                            Qmsplan2File link = em.merge(new Qmsplan2File(sysfile, (Qmsplan) attachable,
                                    OPDE.getLogin().getUser(), new Date()));
                            sysfile.getQmsplanAssignCollection().add(link);
                            ((Qmsplan) attachable).getAttachedFilesConnections().add(link);
                        } else if (attachable instanceof Qms) {
                            Qms2File link = em.merge(new Qms2File(sysfile, (Qms) attachable,
                                    OPDE.getLogin().getUser(), new Date()));
                            sysfile.getQmsAssignCollection().add(link);
                            ((Qms) attachable).getAttachedFilesConnections().add(link);
                        } else if (attachable instanceof Training) {
                            Training2File link = em.merge(new Training2File(sysfile, (Training) attachable,
                                    OPDE.getLogin().getUser(), new Date()));
                            sysfile.getTrainingAssignCollection().add(link);
                            ((Training) attachable).getAttachedFilesConnections().add(link);
                        } else if (attachable instanceof Training2Users) {
                            em.merge(((SYSFilesContainer) attachable).attachFile(sysfile));
                        }
                    }
                    successful.add(sysfile);
                }
                if (successful.size() != files.length) {
                    OPDE.getDisplayManager()
                            .addSubMessage(new DisplayMessage(SYSTools.xx("misc.msg.nodirectories")));
                }
            }
            em.getTransaction().commit();
        } catch (OptimisticLockException ole) {
            OPDE.warn(ole);
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            if (ole.getMessage().indexOf("Class> entity.info.Resident") > -1) {
                OPDE.getMainframe().emptyFrame();
                OPDE.getMainframe().afterLogin();
            }
            // Bereits gespeicherte wieder lschen
            //                for (SYSFiles sysfile : successful) {
            //                    try {
            //                        ftp.deleteFile(sysfile.getRemoteFilename());
            //                    } catch (IOException e) {
            //                        OPDE.fatal(e);
            //                    }
            //                }
            OPDE.getDisplayManager().addSubMessage(DisplayManager.getLockMessage());
        } catch (Exception ex) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            // Bereits gespeicherte wieder lschen
            //                for (SYSFiles sysfile : successful) {
            //                    try {
            //                        ftp.deleteFile(sysfile.getRemoteFilename());
            //                    } catch (IOException e) {
            //                        OPDE.fatal(e);
            //                    }
            //                }
            OPDE.fatal(ex);
        } finally {
            em.close();
            try {
                ftp.disconnect();
            } catch (Exception e) {
                OPDE.error(e);
            }
        }
    }
    return successful;
}

From source file:org.spc.ofp.tubs.domain.purseseine.TripRepository.java

public void update(final PurseSeineTrip trip) {
    final EntityManager mgr = emf.createEntityManager();
    final EntityTransaction xa = mgr.getTransaction();
    try {//from www .  jav  a 2 s  .co  m
        xa.begin();
        mgr.merge(trip);
        xa.commit();
    } catch (Exception ex) {
        if (xa.isActive()) {
            xa.rollback();
        }
    } finally {
        mgr.close();
    }
}

From source file:com.appdynamics.loan.persistence.BasePersistenceImpl.java

/**
 * The method to update the serailizable business objects into the database.
 * /* w  ww  .  j  a  va2 s . c  o m*/
 * @param object --
 *            serializable object
 * @throws PersistenceException
 */
@Transactional
public void update(final Serializable object) {
    EntityManager entityManager = getEntityManager();
    EntityTransaction txn = entityManager.getTransaction();
    txn.begin();
    try {
        entityManager.merge(object);
    } catch (Exception ex) {
        txn.rollback();
    } finally {
        if (!txn.getRollbackOnly()) {
            txn.commit();
        }
    }
}

From source file:de.berlios.jhelpdesk.dao.jpa.TicketFilterDAOJpa.java

@Transactional(readOnly = false)
public void saveOrUpdate(final TicketFilter filter) {
    if (filter.getId() == null) {
        this.jpaTemplate.persist(filter);
    } else {/*from   w w  w.  j  a v a  2s  . c  o  m*/
        this.jpaTemplate.execute(new JpaCallback<TicketFilter>() {
            public TicketFilter doInJpa(EntityManager em) throws PersistenceException {
                TicketFilterDAOJpa.this.deleteFilterItems(em, filter.getId());
                em.merge(filter);
                // WORKAROUND: Jest jaki problem z zapisaniem do bazy pl z dat
                // ustawionych na null, jeli wczeniej miay jak warto... (OpenJPA 1.2.2)
                Query updateQuery = em
                        .createNativeQuery("UPDATE ticket_filters SET begin_date=?1, end_date=?2 WHERE id=?3");
                updateQuery.setParameter(1, filter.getBeginDate());
                updateQuery.setParameter(2, filter.getEndDate());
                updateQuery.setParameter(3, filter.getId());
                updateQuery.executeUpdate();
                return null;
            }
        });
    }
}

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

@Override
public boolean update(UserSkill userSkill) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = true;
    try {/*w ww .  ja  va 2  s. c o  m*/
        em.getTransaction().begin();
        em.merge(userSkill);
        em.getTransaction().commit();
        success = true;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
            success = false;
        }
        em.close();
    }

    return success;
}

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

@Override
public boolean update(VacancySkill vacancySkill) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = true;
    try {//ww w.  j a v a  2  s.c o m
        em.getTransaction().begin();
        em.merge(vacancySkill);
        em.getTransaction().commit();
        success = true;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
            success = false;
        }
        em.close();
    }

    return success;
}