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.thejustdo.servlet.CargaMasivaServlet.java

/**
 * Builds a new dream box based on the initial info.
 *
 * @param em Manages the persistence layer.
 * @param b Buyer owner of this box./*from w  ww . ja v  a 2s  . c  o m*/
 * @param args Set of properties for the buyer as follows: <ul> <li> 0:
 * Office. </li> <li> 1: Open amount. </li> <li> 2: Open user. </li> </ul>
 * @return The built dream box.
 */
private DreamBox buildDreamBox(EntityManager em, Buyer b, String... args) {
    // 0. Parsing parameters.
    String batch = ParametersQueries.getParameterValueByKey(ParametersQueries.BATCH, em);
    long boxCode = DreamBoxQueries.getNextBoxCode(em);
    String office = args[0];
    Double open_amount = Double.parseDouble(args[1]);
    String open_user = args[2];

    // 1 Building the dream box.
    DreamBox box = new DreamBox(boxCode);
    box.setOpenOffice(office);
    box.setOpenAmount(open_amount);
    box.setOpenDate(GregorianCalendar.getInstance().getTime());
    box.setOpenUser(open_user);
    box.setState("activo");
    box.setBoxBatch(batch);
    box.setOwner(b);

    // 2. Sending it to the persistence layer.
    em.merge(box);

    return box;
}

From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.jpa.NewsHandler.java

/**
 * saves or updates a newscategory./*w  w  w .  ja va 2s  .  co  m*/
 *
 * @param metaCategory the category
 * @param em           the em
 * @return the category with the new id
 * @throws Exception the exception
 */
private NewsMetaCategory saveNewsMetaCategory(NewsMetaCategory metaCategory, EntityManager em)
        throws Exception {

    // try loading the category for the firstspirit id
    NewsMetaCategory cat = getNewsMetaCategory(metaCategory.getFs_id(), metaCategory.getLanguage(), em);

    if (cat != null) {
        // update existing category
        cat.setFs_id(metaCategory.getFs_id());
        cat.setLanguage(metaCategory.getLanguage());
        cat.setName(metaCategory.getName());
        cat.setVersion(metaCategory.getVersion());
        cat.setLastmodified(metaCategory.getLastmodified());

        // update
        cat = em.merge(cat);

        return cat;
    } else {
        // save to db
        em.persist(metaCategory);
    }

    return metaCategory;
}

From source file:org.artificer.repository.hibernate.HibernatePersistenceManager.java

@Override
public void updateOntology(final ArtificerOntology ontology) throws ArtificerException {
    new HibernateUtil.HibernateTask<Void>() {
        @Override/*from w  w  w  .ja  v a2s .co m*/
        protected Void doExecute(EntityManager entityManager) throws Exception {
            ArtificerOntology persistedOntology = HibernateUtil.getOntology(ontology.getUuid(), entityManager);

            HibernateUtil.evict(ArtificerOntology.class, ontology.getSurrogateId(), entityManager);

            // Set the surrogate ID
            ontology.setSurrogateId(persistedOntology.getSurrogateId());

            // Don't trust users to properly set both sides of the association...
            for (ArtificerOntologyClass rootClass : ontology.getRootClasses()) {
                if (rootClass.getRoot() == null) {
                    rootClass.setRoot(ontology);
                }
            }
            entityManager.merge(ontology);
            return null;
        }
    }.execute();
}

From source file:in.bookmylab.jpa.JpaDAO.java

public void saveSpmBooking(SpmLabBooking spm) throws AppException {
    EntityManager em = emf.createEntityManager();
    try {/* w  ww.j  a  va 2  s  . co  m*/
        em.getTransaction().begin();
        if (spm.spmId == null) {
            em.persist(spm);
            fixAnalysisModesAndResourceType(spm.booking, em, false);
        } else {
            // Validate status change, if any
            Query q = em.createNamedQuery("ResourceBooking.findById");
            ResourceBooking rb = (ResourceBooking) q.setParameter("bookingId", spm.booking.bookingId)
                    .getSingleResult();
            if (!statusChangeValid(rb.status, spm.booking.status)) {
                throw new AppException(String.format("Status cannot be changed from %s to %s.", rb.status,
                        spm.booking.status));
            }

            fixAnalysisModesAndResourceType(spm.booking, em, true);
            em.merge(spm);
        }
        em.getTransaction().commit();
    } finally {
        em.close();
    }
}

From source file:in.bookmylab.jpa.JpaDAO.java

public void saveXrdBooking(XrdLabBooking xrd) throws AppException {
    EntityManager em = emf.createEntityManager();
    try {/*from w w  w  . java  2 s .  c om*/
        em.getTransaction().begin();
        if (xrd.xrdId == null) {
            em.persist(xrd);
            fixAnalysisModesAndResourceType(xrd.booking, em, false);
        } else {
            // Validate status change, if any
            Query q = em.createNamedQuery("ResourceBooking.findById");
            ResourceBooking rb = (ResourceBooking) q.setParameter("bookingId", xrd.booking.bookingId)
                    .getSingleResult();
            if (!statusChangeValid(rb.status, xrd.booking.status)) {
                throw new AppException(String.format("Status cannot be changed from %s to %s.", rb.status,
                        xrd.booking.status));
            }

            fixAnalysisModesAndResourceType(xrd.booking, em, true);
            em.merge(xrd);
        }
        em.getTransaction().commit();
    } finally {
        em.close();
    }
}

From source file:it.attocchi.jpa2.JpaController.java

public <T extends Serializable> void update(T o) throws Exception {

    EntityManager em = getEntityManager();

    try {//from www.ja v  a 2  s  . c o  m
        if (!globalTransactionOpen)
            em.getTransaction().begin();
        em.merge(o);
        if (!globalTransactionOpen)
            em.getTransaction().commit();

    } catch (Exception e) {
        throw e;
    } finally {
        // Close the database connection:
        if (!globalTransactionOpen) {
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
            closeEm(); // em.close();
        }
    }

}

From source file:portal.api.impl.PortalJpaController.java

public PortalUser updatePortalUser(PortalUser bu) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();/*from   w w w  .  ja  va 2  s .co m*/
    PortalUser resis = entityManager.merge(bu);
    entityTransaction.commit();

    return resis;
}

From source file:portal.api.impl.PortalJpaController.java

public PortalProperty updateProperty(PortalProperty p) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();//  ww w .  j  av  a  2  s.  c  o m
    PortalProperty bp = entityManager.merge(p);
    entityTransaction.commit();

    return bp;
}

From source file:portal.api.impl.PortalJpaController.java

public InstalledVxF updateInstalledVxF(InstalledVxF is) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();/*from   w  w  w.  j a va2 s  .c o  m*/
    InstalledVxF resis = entityManager.merge(is);
    entityTransaction.commit();

    return resis;
}

From source file:portal.api.impl.PortalJpaController.java

public MANOplatform updateMANOplatform(MANOplatform c) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();/* w ww.j  a  v  a  2  s. co  m*/
    MANOplatform resis = entityManager.merge(c);
    entityTransaction.commit();

    return resis;
}