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.mitre.util.jpa.JpaUtil.java

public static <T, I> T saveOrUpdate(I id, EntityManager entityManager, T entity) {
    if (id == null) {
        entityManager.persist(entity);//  w  w  w  .ja v a2s .c o  m
        entityManager.flush();
        return entity;
    } else {
        T tmp = entityManager.merge(entity);
        entityManager.flush();
        return tmp;
    }
}

From source file:org.opentides.dao.impl.AuditLogDaoImpl.java

/**
 * Saves the log event into the database.
 * @param shortMessage/*from  w w  w.  jav  a 2 s . c o  m*/
 * @param message
 * @param entity
 */
public static void logEvent(String message, BaseEntity entity) {
    Long userId = entity.getAuditUserId();
    String username = entity.getAuditUsername();

    if (ApplicationStartupListener.isApplicationStarted()) {
        if (userId == null) {
            _log.error("No userId specified for audit logging on object [" + entity.getClass().getName()
                    + "] for message [" + message + "]. Retrieving user from interceptor.");
            SessionUser user = SecurityUtil.getSessionUser();
            userId = user.getId();
            username = user.getUsername();
        }
    } else {
        userId = new Long(0);
        username = "System Evolve";
    }

    EntityManager em = DatabaseUtil.getEntityManager();
    try {
        em.getTransaction().begin();
        AuditLog record = new AuditLog(message, entity.getId(), entity.getClass(), entity.getReference(),
                userId, username);
        em.persist(record);
        em.flush();
        em.getTransaction().commit();
    } finally {
        if (em != null && em.isOpen()) {
            em.close();
        }
    }
}

From source file:org.opentides.persistence.interceptor.SynchronizableInterceptor.java

/**
 * Saves the change log into the database.
 * @param shortMessage//from w w w  .j a v  a  2 s.  c  o  m
 * @param message
 * @param entity
 */
public static void saveLog(BaseEntity entity, int action, String updateFields, String sqlCommand) {
    EntityManager em = DatabaseUtil.getEntityManager();
    try {
        em.getTransaction().begin();
        ChangeLog cl = new ChangeLog(entity.getId(), entity.getClass(), action, updateFields, sqlCommand);
        em.persist(cl);
        em.flush();
        em.getTransaction().commit();
    } catch (Exception ex) {
        _log.error("Failed to save change log on [" + entity.getClass().getSimpleName() + "]", ex);
    } finally {
        if (em != null && em.isOpen()) {
            em.close();
        }
    }
}

From source file:nl.b3p.kaartenbalie.service.GroupParser.java

public static void addAllLayersToGroup(Organization org, ServiceProvider sp, EntityManager em)
        throws Exception {
    Set wmsLayers = new HashSet();

    Set<Layer> orgWmsLayerSet = org.getLayers();
    for (Layer l : orgWmsLayerSet) {
        ServiceProvider layerSp = l.getServiceProvider();

        if (!layerSp.getAbbr().equals(sp.getAbbr())) {
            wmsLayers.add(l);/*from  w w w. j a v  a2s  . c o  m*/
        }
    }

    Set<Layer> selectedLayers = sp.getAllLayers();
    for (Layer l : selectedLayers) {
        wmsLayers.add(l);
    }

    org.setLayers(wmsLayers);

    em.merge(org);
    em.flush();
}

From source file:nl.b3p.kaartenbalie.core.server.accounting.AccountManager.java

/**
 * //from  www .jav  a  2s.  c  o m
 * @param organizationId
 * @return
 */
public synchronized static AccountManager getAccountManager(Integer organizationId) throws Exception {
    AccountManager accountManager = (AccountManager) managers.get(organizationId);
    if (accountManager == null) {
        accountManager = new AccountManager(organizationId);
        if (enableAccounting) {
            log.debug("Getting entity manager ......");
            EntityManager em = MyEMFDatabase.getEntityManager(MyEMFDatabase.MAIN_EM);
            Account account = (Account) em.find(Account.class, organizationId);
            if (account == null) {
                Organization organization = (Organization) em.find(Organization.class, organizationId);
                account = new Account(organization);
                organization.setAccount(account);
                em.persist(account);
                em.flush();
            }
            managers.put(organizationId, accountManager);
        }
    }
    return accountManager;
}

From source file:com.vladmihalcea.CleanDbUtil.java

public static void cleanStore(final TransactionTemplate transactionTemplate,
        final EntityManager entityManager) {
    transactionTemplate.execute(new TransactionCallback<Void>() {
        @Override/*from  w w  w  . ja  v  a 2s  .co m*/
        public Void doInTransaction(TransactionStatus transactionStatus) {
            entityManager.createQuery("delete from SubVersion where id > 0").executeUpdate();
            entityManager.createQuery("delete from Version where id > 0").executeUpdate();
            entityManager.createQuery("delete from Image where id > 0").executeUpdate();
            entityManager.createQuery("delete from WarehouseProductInfo where id > 0").executeUpdate();
            entityManager.createQuery("delete from Product where id > 0").executeUpdate();
            entityManager.createQuery("delete from Company where id > 0").executeUpdate();
            entityManager.flush();
            return null;
        }
    });
}

From source file:nl.b3p.kaartenbalie.service.GroupParser.java

public static void addAllLayersToGroup(Organization org, WfsServiceProvider sp, EntityManager em)
        throws Exception {
    log.info("Updating WFS rights for :" + org.getName());

    Set wfsLayers = new HashSet();

    Set<WfsLayer> orgWfsLayerSet = org.getWfsLayers();
    for (WfsLayer l : orgWfsLayerSet) {
        WfsServiceProvider layerSp = l.getWfsServiceProvider();

        if (!layerSp.getAbbr().equals(sp.getAbbr())) {
            wfsLayers.add(l);/*from  w  w  w  .j  a v a  2  s .c  om*/

            log.info("Org wfs layer :" + l.getName());
        }
    }

    Set<WfsLayer> selectedLayers = sp.getWfsLayers();
    for (WfsLayer l : selectedLayers) {
        wfsLayers.add(l);

        log.info("Wfs layer :" + l.getName());
    }

    org.setWfsLayers(wfsLayers);

    em.merge(org);
    em.flush();
}

From source file:com.gerenciaProyecto.DaoImple.VentaDaoImpl.java

@Override
@Transactional/*from   w w w . j a va  2 s . c o  m*/
public void AgregarVenta(Venta p_Venta) {
    EntityManager em = getEntityManager();
    try {
        em.persist(p_Venta);
        em.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:uk.co.threeonefour.ifictionary.web.user.dao.JpaUserDao.java

@Transactional
@Override
public User update(User user) {

    EntityManager em = entityManager;
    em.merge(user);
    em.flush();
    return user;
}

From source file:uk.co.threeonefour.ifictionary.web.user.dao.JpaUserDao.java

@Transactional
@Override
public User add(User user) {

    EntityManager em = entityManager;
    em.persist(user);
    em.flush();
    return user;
}