Example usage for javax.persistence EntityManager flush

List of usage examples for javax.persistence EntityManager flush

Introduction

In this page you can find the example usage for javax.persistence EntityManager flush.

Prototype

public void flush();

Source Link

Document

Synchronize the persistence context to the underlying database.

Usage

From source file:org.mule.module.jpa.command.Merge.java

public Object execute(EntityManager entityManager, Object entity, Map<String, Object> parameters, Boolean flush)
        throws Exception {

    logger.debug("Merging entity: " + entity);

    entityManager.merge(entity);/*from  w  w w.  ja va  2  s.  c  o  m*/

    if (flush) {
        entityManager.flush();
    }
    return entity;
}

From source file:org.mule.module.jpa.command.Persist.java

public Object execute(EntityManager entityManager, Object entity, Map<String, Object> parameters, Boolean flush)
        throws Exception {

    logger.debug("Persisting entity: " + entity);

    entityManager.persist(entity);//from   www.ja v  a2s  . co  m

    if (flush) {
        entityManager.flush();
    }

    return entity;
}

From source file:org.rhq.enterprise.server.drift.DriftManagerBeanTest.java

private void deleteDriftFiles() throws Exception {
    getTransactionManager().begin();//from   w  ww . jav a  2  s .  c o  m
    EntityManager em = getEntityManager();

    try {
        try {
            // wipe out any test DriftFiles (the test files have sha256 0,1,...)
            for (int i = 0, numDeleted = 1; (numDeleted > 0); ++i) {
                numDeleted = getEntityManager()
                        .createQuery("delete from JPADriftFile where hash_id = '" + i + "'").executeUpdate();
            }
        } catch (Exception e) {
            System.out.println("CANNOT PREPARE TEST: " + e);
            getTransactionManager().rollback();
            throw e;
        }

        em.flush();
        getTransactionManager().commit();
    } finally {
        em.close();
    }
}

From source file:commonSession.PersistAttivitaFacadeBean.java

public void delete(Object id, Class clazz, String unit) throws Exception {

    //    EntityManager em;
    EntityManager em = GestionaleEm;
    if (unit.equals("GestionaleEm"))
        em = GestionaleEm;//from  www  .  j ava  2 s  .  co m
    else
        em = TerritorioEm;

    Object obj = em.find(clazz, id);
    em.remove(obj);
    em.flush();

}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public UserSession saveUserSession(UserSession userSession) {
    logger.info("Will userSession = " + userSession.getUsername());

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();//from w  w w.  j a  v a  2s . co  m

    FStoreUser u = entityManager.find(FStoreUser.class, userSession.getUser().getId());
    userSession.setUser(u);

    logger.info("Will userSession = " + u.toString());

    entityManager.persist(u);
    entityManager.persist(userSession);
    entityManager.flush();
    entityTransaction.commit();

    return userSession;

}

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

@Test(enabled = true, invocationCount = 1)
public void testNassword0() {
    final EntityManager manager = LocalPU.createEntityManager();
    try {//from   w w w  . j  av a2 s  .  co m
        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:gr.upatras.ece.nam.baker.impl.BakerJpaController.java

public void deleteAllUsers() {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();/*  w  w w .ja  va  2s.c om*/

    Query q = entityManager.createQuery("DELETE FROM BakerUser ");
    q.executeUpdate();
    entityManager.flush();

    entityTransaction.commit();

}

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

public void deleteAllInstalledBuns() {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();/*from w ww .j a v a  2 s  .c  o  m*/

    Query q = entityManager.createQuery("DELETE FROM InstalledBun ");
    q.executeUpdate();
    entityManager.flush();

    entityTransaction.commit();
}

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

public void deleteAllSubscribedResources() {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();//from  w ww.ja  v a 2 s. c o m

    Query q = entityManager.createQuery("DELETE FROM SubscribedResource ");
    q.executeUpdate();
    entityManager.flush();

    entityTransaction.commit();

}

From source file:nl.b3p.kaartenbalie.core.server.monitoring.DataMonitoring.java

public void endClientRequest(String service, String operation, int bytesSentToUser, long totalResponseTime) {

    if (!isEnableMonitoring()) {
        return;/*from  w w  w . j  av a 2 s.c o  m*/
    }
    if (clientRequest == null) {
        return;
    }

    try {
        log.debug("Getting entity manager ......");
        EntityManager em = MyEMFDatabase.getEntityManager(MyEMFDatabase.MAIN_EM);

        requestOperation.setDuration(new Long(totalResponseTime));
        requestOperation.setBytesSentToUser(new Integer(bytesSentToUser));

        this.addRequestOperation(requestOperation);
        clientRequest.setService(service);
        clientRequest.setOperation(operation);
        clientRequest.setUser(user);
        clientRequest.setOrganization(organization);
        //Now Persist...
        Iterator iterRO = clientRequest.getRequestOperations().iterator();
        while (iterRO.hasNext()) {
            em.persist(iterRO.next());
        }
        Iterator iterSPR = clientRequest.getServiceProviderRequests().iterator();
        while (iterSPR.hasNext()) {
            em.persist(iterSPR.next());
        }
        em.persist(clientRequest);
        em.flush();
    } catch (Exception e) {
        log.error("Error logging request", e);
    }

    clientRequest = null;
}