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:gr.upatras.ece.nam.baker.impl.BakerJpaController.java

public InstalledBun updateInstalledBun(InstalledBun is) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();//w  w w.j a va2 s .  co m
    InstalledBun resis = entityManager.merge(is);
    entityTransaction.commit();

    return resis;
}

From source file:gr.upatras.ece.nam.baker.impl.BakerJpaController.java

public SubscribedResource updateSubscribedResource(SubscribedResource sm) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();/*from w w  w . j a v a 2  s .  co  m*/
    SubscribedResource resis = entityManager.merge(sm);
    entityTransaction.commit();

    return resis;
}

From source file:gr.upatras.ece.nam.baker.impl.BakerJpaController.java

public DeploymentDescriptor updateDeploymentDescriptor(DeploymentDescriptor d) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();// ww w  . j  av a 2s.  co m
    DeploymentDescriptor resis = entityManager.merge(d);
    entityTransaction.commit();

    return resis;
}

From source file:com.github.jinahya.persistence.ShadowTest.java

@Test(enabled = true, invocationCount = 1)
public void testNassword0() {
    final EntityManager manager = LocalPU.createEntityManager();
    try {//  w  ww. j a  v  a 2 s . c  om
        final EntityTransaction transaction = manager.getTransaction();
        transaction.begin();
        try {
            final String username = newUsername(manager);
            final byte[] password = newPassword();
            Shadow shadow = persistInstance(manager, username, password);
            Assert.assertTrue(shadow.puthenticate(shadow, password));
            System.out.println("=========================================");
            LOGGER.log(Level.INFO, "mortons: {0}", MORTONS(manager, 0, 1024));
            final byte[] nassword = newPassword();
            shadow.nassword(shadow, password, nassword);
            shadow = manager.merge(shadow);
            manager.flush();
            System.out.println("=========================================");
            LOGGER.log(Level.INFO, "mortons: {0}", MORTONS(manager, 0, 1024));
            Assert.assertFalse(shadow.puthenticate(shadow, password));
            Assert.assertTrue(shadow.puthenticate(shadow, nassword));
            transaction.commit();
        } catch (Exception e) {
            transaction.rollback();
            e.printStackTrace(System.err);
            Assert.fail(e.getMessage());
        }
    } finally {
        manager.close();
    }
}

From source file:org.opencastproject.comments.events.persistence.EventCommentDatabaseServiceImpl.java

@Override
public Comment updateComment(String eventId, Comment comment) throws EventCommentDatabaseException {
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {/*from www  . ja  va2s  . c o m*/
        tx.begin();
        CommentDto updatedComment = CommentDatabaseUtils.mergeComment(comment, em);

        EventCommentDto dto = getEventComment(eventId, updatedComment.getId(), em);
        if (dto == null) {
            dto = new EventCommentDto(eventId, updatedComment, securityService.getOrganization().getId());
            em.persist(dto);
        } else {
            dto.setComment(updatedComment);
            em.merge(dto);
        }
        tx.commit();
        comment = updatedComment.toComment(userDirectoryService);
        sendMessageUpdate(eventId);
        return comment;
    } catch (Exception e) {
        logger.error("Could not update or store comment: {}", ExceptionUtils.getStackTrace(e));
        if (tx.isActive())
            tx.rollback();

        throw new EventCommentDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
}

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

private void save() {
    EntityManager em = OPDE.createEM();

    try {//from w  w  w .j  a  v  a2s  .  co m
        em.getTransaction().begin();

        em.lock(em.merge(resident), LockModeType.OPTIMISTIC);

        // Wenn die aPackage null ist, dann ist eine Sonderpackung
        if (aPackage != null) {
            aPackage = em.merge(aPackage);
            if (amount == null) {
                amount = aPackage.getContent();
            }
        }

        tradeForm = em.merge(tradeForm);
        inventory = em.merge(inventory);

        if (inventory.getID() == null) { // create a new MedInvetory.
            inventory.setText(TradeFormTools.toPrettyString(tradeForm) + "; "
                    + ACMETools.toPrettyStringShort(tradeForm.getMedProduct().getACME()));
        }

        BigDecimal estimatedUPR = MedStockTools.getEstimatedUPR(tradeForm);
        int dummyMode = tradeForm.getDosageForm().isUPRn() && MedStockTools.isNoStockYetForThis(tradeForm)
                ? MedStockTools.REPLACE_WITH_EFFECTIVE_UPR_WHEN_CLOSING
                : MedStockTools.ADD_TO_AVERAGES_UPR_WHEN_CLOSING;

        MedStock newStock = em.merge(
                new MedStock(inventory, tradeForm, aPackage, txtBemerkung.getText(), estimatedUPR, dummyMode));
        newStock.setExpires(expiry);
        MedStockTransaction tx = em.merge(new MedStockTransaction(newStock, amount));
        tx.setWeight(weight);

        em.getTransaction().commit();
        amount = null;
        aPackage = null;
        tradeForm = null;
        inventory = null;
        expiry = null;
        weight = null;

        if (btnPrint.isSelected()) {
            OPDE.getPrintProcessor().addPrintJob(new PrintListElement(newStock, logicalPrinter, printForm,
                    OPDE.getProps().getProperty(SYSPropsTools.KEY_PHYSICAL_PRINTER)));
        }

        // if the label printer is not used, the new number is shown until the next message, so the user has time to write the number down manually.
        OPDE.getDisplayManager()
                .addSubMessage(new DisplayMessage(
                        SYSTools.xx("newstocks.registration.success.1") + " <b>" + newStock.getID() + "</b> "
                                + SYSTools.xx("newstocks.registration.success.2"),
                        btnPrint.isSelected() ? 2 : 0));
    } 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();
        }
        OPDE.getDisplayManager().addSubMessage(DisplayManager.getLockMessage());
    } catch (Exception ex) {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        OPDE.fatal(ex);
    } finally {
        em.close();
    }
}

From source file:uk.ac.edukapp.service.WidgetProfileService.java

public Message addCategory(Widgetprofile widgetProfile, Category category) {
    Message message = new Message();
    message.setMessage("OK");

    EntityManager em = getEntityManagerFactory().createEntityManager();
    em.getTransaction().begin();/*from ww  w . ja va2 s .c o  m*/
    widgetProfile.addCategory(category);
    em.merge(widgetProfile);
    //em.persist(widgetProfile);
    em.getTransaction().commit();
    em.close();

    return message;
}

From source file:uk.ac.edukapp.service.WidgetProfileService.java

public Message removeCategory(Widgetprofile widgetProfile, Category category) {
    Message message = new Message();
    message.setMessage("OK");
    EntityManager em = getEntityManagerFactory().createEntityManager();
    em.getTransaction().begin();/*from   ww  w  .j av a 2s. c o  m*/
    widgetProfile.removeCategory(category);
    em.merge(widgetProfile);
    em.getTransaction().commit();
    em.close();
    return message;
}

From source file:uk.ac.edukapp.service.WidgetProfileService.java

/**
 * Update the description associated with a widget profile
 * /*from  ww w .  ja v a  2s. c om*/
 * @param widgetProfile
 * @param text
 */
private void updateDescription(Widgetprofile widgetProfile, String text) {
    WidgetDescription widget_desc = widgetProfile.getDescription();
    if (widget_desc == null) {
        widget_desc = new WidgetDescription();
        widget_desc.setWid_id(widgetProfile.getId());
    }
    widget_desc.setDescription(text);
    widgetProfile.setDescription(widget_desc);

    EntityManager entityManager = getEntityManagerFactory().createEntityManager();
    entityManager.getTransaction().begin();
    entityManager.persist(entityManager.merge(widgetProfile));
    entityManager.getTransaction().commit();
    entityManager.close();
}

From source file:org.traccar.web.server.model.DataServiceImpl.java

@Override
public Device updateDevice(Device device) {
    EntityManager entityManager = getSessionEntityManager();
    synchronized (entityManager) {
        entityManager.getTransaction().begin();
        try {//  w ww . j  a va  2 s  . c o m
            device = entityManager.merge(device);
            entityManager.getTransaction().commit();
            return device;
        } catch (RuntimeException e) {
            entityManager.getTransaction().rollback();
            throw e;
        }
    }
}