Example usage for javax.persistence FlushModeType COMMIT

List of usage examples for javax.persistence FlushModeType COMMIT

Introduction

In this page you can find the example usage for javax.persistence FlushModeType COMMIT.

Prototype

FlushModeType COMMIT

To view the source code for javax.persistence FlushModeType COMMIT.

Click Source Link

Document

Flushing to occur at transaction commit.

Usage

From source file:gov.guilin.dao.impl.ProductDaoImpl.java

public List<Object[]> findSalesList(Date beginDate, Date endDate, Integer count) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class);
    Root<Product> product = criteriaQuery.from(Product.class);
    Join<Product, OrderItem> orderItems = product.join("orderItems");
    Join<Product, gov.guilin.entity.Order> order = orderItems.join("order");
    criteriaQuery.multiselect(product.get("id"), product.get("sn"), product.get("name"),
            product.get("fullName"), product.get("price"),
            criteriaBuilder.sum(orderItems.<Integer>get("quantity")), criteriaBuilder.sum(criteriaBuilder
                    .prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price"))));
    Predicate restrictions = criteriaBuilder.conjunction();
    if (beginDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.greaterThanOrEqualTo(order.<Date>get("createDate"), beginDate));
    }//from w ww. java 2 s .c o  m
    if (endDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.lessThanOrEqualTo(order.<Date>get("createDate"), endDate));
    }
    restrictions = criteriaBuilder.and(restrictions,
            criteriaBuilder.equal(order.get("orderStatus"), OrderStatus.completed),
            criteriaBuilder.equal(order.get("paymentStatus"), PaymentStatus.paid));
    criteriaQuery.where(restrictions);
    criteriaQuery.groupBy(product.get("id"), product.get("sn"), product.get("name"), product.get("fullName"),
            product.get("price"));
    criteriaQuery.orderBy(criteriaBuilder.desc(criteriaBuilder.sum(
            criteriaBuilder.prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price")))));
    TypedQuery<Object[]> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT);
    if (count != null && count >= 0) {
        query.setMaxResults(count);
    }
    return query.getResultList();
}

From source file:net.shopxx.dao.impl.BaseDaoImpl.java

@SuppressWarnings("rawtypes")
public void update(Long[] ids, Map<String, Object> map, T entity) {
    String className = entity.getClass().getSimpleName();
    Set set = map.keySet();//from  w w  w  .  j a v a2  s  .  c o  m
    List<Long> fileList = Arrays.asList(ids);
    if (ids.length > 0 && set.size() > 0) {
        String jpql = "update " + className + " entity set";
        for (Iterator iter = set.iterator(); iter.hasNext();) {
            String key = (String) iter.next();
            jpql += " entity." + key + "=" + map.get(key);
            if (iter.hasNext()) {
                jpql += ",";
            }
        }
        jpql += " where entity.id in (:ids)";
        entityManager.createQuery(jpql).setFlushMode(FlushModeType.COMMIT).setParameter("ids", fileList)
                .executeUpdate();
    }
}

From source file:com.impetus.kundera.persistence.EntityManagerImpl.java

private void onLookUp(PersistenceUnitTransactionType transactionType) {
    // TODO transaction should not be null;
    if (transactionType != null && transactionType.equals(PersistenceUnitTransactionType.JTA)) {
        if (this.entityTransaction == null) {
            this.entityTransaction = new KunderaEntityTransaction(this);
        }/* w ww.  ja v a  2s .  c om*/
        Context ctx;
        try {
            ctx = new InitialContext();

            this.utx = (UserTransaction) ctx.lookup("java:comp/UserTransaction");

            if (this.utx == null) {
                throw new KunderaException(
                        "Lookup for UserTransaction returning null for :{java:comp/UserTransaction}");
            }
            // TODO what is need to check?
            if (!(this.utx instanceof KunderaJTAUserTransaction)) {
                throw new KunderaException("Please bind [" + KunderaJTAUserTransaction.class.getName()
                        + "] for :{java:comp/UserTransaction} lookup" + this.utx.getClass());
            }

            if (!this.entityTransaction.isActive()) {
                this.entityTransaction.begin();
                this.setFlushMode(FlushModeType.COMMIT);
                ((KunderaJTAUserTransaction) this.utx).setImplementor(this);
            }

        } catch (NamingException e) {
            logger.error("Error during initialization of entity manager, Caused by:", e);
            throw new KunderaException(e);
        }

    }
}

From source file:net.groupbuy.dao.impl.ProductDaoImpl.java

public boolean isPurchased(Member member, Product product) {
    if (member == null || product == null) {
        return false;
    }/* w  w w . j  a  v  a2 s. c  o m*/
    String jqpl = "select count(*) from OrderItem orderItem where orderItem.product = :product and orderItem.order.member = :member and orderItem.order.orderStatus = :orderStatus";
    Long count = entityManager.createQuery(jqpl, Long.class).setFlushMode(FlushModeType.COMMIT)
            .setParameter("product", product).setParameter("member", member)
            .setParameter("orderStatus", OrderStatus.completed).getSingleResult();
    return count > 0;
}

From source file:net.groupbuy.dao.impl.ProductDaoImpl.java

/**
 * //from  w  w  w.j  a v  a 2 s  .  co m
 * 
 * @param product
 *            ?
 * @return ?
 */
@Override
public Product merge(Product product) {
    Assert.notNull(product);

    if (!product.getIsGift()) {
        String jpql = "delete from GiftItem giftItem where giftItem.gift = :product";
        entityManager.createQuery(jpql).setFlushMode(FlushModeType.COMMIT).setParameter("product", product)
                .executeUpdate();
    }
    if (!product.getIsMarketable() || product.getIsGift()) {
        String jpql = "delete from CartItem cartItem where cartItem.product = :product";
        entityManager.createQuery(jpql).setFlushMode(FlushModeType.COMMIT).setParameter("product", product)
                .executeUpdate();
    }
    setValue(product);
    return super.merge(product);
}

From source file:org.apache.oozie.test.XTestCase.java

private void cleanUpDBTablesInternal() throws StoreException {
    EntityManager entityManager = Services.get().get(JPAService.class).getEntityManager();
    entityManager.setFlushMode(FlushModeType.COMMIT);
    entityManager.getTransaction().begin();

    Query q = entityManager.createNamedQuery("GET_WORKFLOWS");
    List<WorkflowJobBean> wfjBeans = q.getResultList();
    int wfjSize = wfjBeans.size();
    for (WorkflowJobBean w : wfjBeans) {
        entityManager.remove(w);/*  www . j a va2 s.c  o m*/
    }

    q = entityManager.createNamedQuery("GET_ACTIONS");
    List<WorkflowActionBean> wfaBeans = q.getResultList();
    int wfaSize = wfaBeans.size();
    for (WorkflowActionBean w : wfaBeans) {
        entityManager.remove(w);
    }

    q = entityManager.createNamedQuery("GET_COORD_JOBS");
    List<CoordinatorJobBean> cojBeans = q.getResultList();
    int cojSize = cojBeans.size();
    for (CoordinatorJobBean w : cojBeans) {
        entityManager.remove(w);
    }

    q = entityManager.createNamedQuery("GET_COORD_ACTIONS");
    List<CoordinatorActionBean> coaBeans = q.getResultList();
    int coaSize = coaBeans.size();
    for (CoordinatorActionBean w : coaBeans) {
        entityManager.remove(w);
    }

    q = entityManager.createNamedQuery("GET_BUNDLE_JOBS");
    List<BundleJobBean> bjBeans = q.getResultList();
    int bjSize = bjBeans.size();
    for (BundleJobBean w : bjBeans) {
        entityManager.remove(w);
    }

    q = entityManager.createNamedQuery("GET_BUNDLE_ACTIONS");
    List<BundleActionBean> baBeans = q.getResultList();
    int baSize = baBeans.size();
    for (BundleActionBean w : baBeans) {
        entityManager.remove(w);
    }

    q = entityManager.createNamedQuery("GET_SLA_EVENTS");
    List<SLAEventBean> slaBeans = q.getResultList();
    int slaSize = slaBeans.size();
    for (SLAEventBean w : slaBeans) {
        entityManager.remove(w);
    }

    q = entityManager.createQuery("select OBJECT(w) from SLARegistrationBean w");
    List<SLARegistrationBean> slaRegBeans = q.getResultList();
    int slaRegSize = slaRegBeans.size();
    for (SLARegistrationBean w : slaRegBeans) {
        entityManager.remove(w);
    }

    q = entityManager.createQuery("select OBJECT(w) from SLASummaryBean w");
    List<SLASummaryBean> sdBeans = q.getResultList();
    int ssSize = sdBeans.size();
    for (SLASummaryBean w : sdBeans) {
        entityManager.remove(w);
    }

    entityManager.getTransaction().commit();
    entityManager.close();
    log.info(wfjSize + " entries in WF_JOBS removed from DB!");
    log.info(wfaSize + " entries in WF_ACTIONS removed from DB!");
    log.info(cojSize + " entries in COORD_JOBS removed from DB!");
    log.info(coaSize + " entries in COORD_ACTIONS removed from DB!");
    log.info(bjSize + " entries in BUNDLE_JOBS removed from DB!");
    log.info(baSize + " entries in BUNDLE_ACTIONS removed from DB!");
    log.info(slaSize + " entries in SLA_EVENTS removed from DB!");
    log.info(slaRegSize + " entries in SLA_REGISTRATION removed from DB!");
    log.info(ssSize + " entries in SLA_SUMMARY removed from DB!");

}

From source file:org.apache.oozie.tools.OozieDBExportCLI.java

private static void queryAllDBTables(String filename) throws StoreException, IOException {

    EntityManager manager = null;//  w  w w.j  av  a2 s . c o  m
    ZipOutputStream zos = null;
    File file = null;
    try {
        file = new File(filename);
        zos = new ZipOutputStream(new FileOutputStream(file));
        zos.setLevel(1);
        manager = Services.get().get(JPAService.class).getEntityManager();
        manager.setFlushMode(FlushModeType.COMMIT);

        int infoSize = exportTableToJSON(manager.createNativeQuery(GET_DB_VERSION), zos, OOZIEDB_SYS_INFO_JSON);
        System.out.println(infoSize + " rows exported from OOZIE_SYS");

        int wfjSize = exportTableToJSON(manager.createQuery(GET_WORKFLOW_JOBS), zos, OOZIEDB_WF_JSON);
        System.out.println(wfjSize + " rows exported from WF_JOBS");

        int wfaSize = exportTableToJSON(manager.createQuery(GET_WORKFLOW_ACTIONS), zos, OOZIEDB_AC_JSON);
        System.out.println(wfaSize + " rows exported from WF_ACTIONS");

        int cojSize = exportTableToJSON(manager.createQuery(GET_COORD_JOBS), zos, OOZIEDB_CJ_JSON);
        System.out.println(cojSize + " rows exported from COORD_JOBS");

        int coaSize = exportTableToJSON(manager.createQuery(GET_COORD_ACTIONS), zos, OOZIEDB_CA_JSON);
        System.out.println(coaSize + " rows exported from COORD_ACTIONS");

        int bnjSize = exportTableToJSON(manager.createQuery(GET_BUNDLE_JOBS), zos, OOZIEDB_BNJ_JSON);
        System.out.println(bnjSize + " rows exported from BUNDLE_JOBS");

        int bnaSize = exportTableToJSON(manager.createQuery(GET_BUNDLE_ACIONS), zos, OOZIEDB_BNA_JSON);
        System.out.println(bnaSize + " rows exported from BUNDLE_ACTIONS");

        int slaRegSize = exportTableToJSON(manager.createQuery(GET_SLA_REGISTRATIONS), zos,
                OOZIEDB_SLAREG_JSON);
        System.out.println(slaRegSize + " rows exported from SLA_REGISTRATION");

        int ssSize = exportTableToJSON(manager.createQuery(GET_SLA_SUMMARYS), zos, OOZIEDB_SLASUM_JSON);
        System.out.println(ssSize + " rows exported from SLA_SUMMARY");

    } catch (Exception e) {
        System.err.println("Error during dump creation: " + e.getMessage());
        System.err.println();
        e.printStackTrace(System.err);
        System.err.println();
        if (file != null) {
            file.delete();
        }
        System.exit(1);
    } finally {
        IOUtils.closeSafely(zos);
        if (manager != null) {
            manager.close();
        }
    }
}

From source file:org.apache.oozie.tools.OozieDBImportCLI.java

private static void importAllDBTables(String zipFileName)
        throws StoreException, IOException, JPAExecutorException {

    EntityManager entityManager = null;// w w w .j  a  v  a 2 s . co  m
    ZipFile zipFile = null;
    try {

        entityManager = Services.get().get(JPAService.class).getEntityManager();
        entityManager.setFlushMode(FlushModeType.COMMIT);
        zipFile = new ZipFile(zipFileName);
        checkDBVersion(entityManager, zipFile);
        importFrom(entityManager, zipFile, "WF_JOBS", WorkflowJobBean.class, OOZIEDB_WF_JSON);
        importFrom(entityManager, zipFile, "WF_ACTIONS", WorkflowActionBean.class, OOZIEDB_AC_JSON);
        importFrom(entityManager, zipFile, "COORD_JOBS", CoordinatorJobBean.class, OOZIEDB_CJ_JSON);
        importFrom(entityManager, zipFile, "COORD_ACTIONS", CoordinatorActionBean.class, OOZIEDB_CA_JSON);
        importFrom(entityManager, zipFile, "BUNDLE_JOBS", BundleJobBean.class, OOZIEDB_BNJ_JSON);
        importFrom(entityManager, zipFile, "BUNDLE_ACTIONS", BundleActionBean.class, OOZIEDB_BNA_JSON);
        importFrom(entityManager, zipFile, "SLA_REGISTRATION", SLARegistrationBean.class, OOZIEDB_SLAREG_JSON);
        importFrom(entityManager, zipFile, "SLA_SUMMARY", SLASummaryBean.class, OOZIEDB_SLASUM_JSON);

    } finally {
        if (entityManager != null) {
            entityManager.close();
        }
        if (zipFile != null) {
            zipFile.close();
        }
    }

}

From source file:org.apache.openjpa.persistence.EntityManagerImpl.java

/**
 * Translate our internal flush constant to a flush mode enum value.
 *//*from   w  ww.j a  va 2s . c om*/
static FlushModeType fromFlushBeforeQueries(int flush) {
    switch (flush) {
    case QueryFlushModes.FLUSH_TRUE:
        return FlushModeType.AUTO;
    case QueryFlushModes.FLUSH_FALSE:
        return FlushModeType.COMMIT;
    default:
        return null;
    }
}

From source file:org.apache.openjpa.persistence.EntityManagerImpl.java

/**
 * Translate a flush mode enum value to our internal flush constant.
 *//*from   ww  w.ja va  2 s.  c o m*/
static int toFlushBeforeQueries(FlushModeType flushMode) {
    // choose default for null
    if (flushMode == null)
        return QueryFlushModes.FLUSH_WITH_CONNECTION;
    if (flushMode == FlushModeType.AUTO)
        return QueryFlushModes.FLUSH_TRUE;
    if (flushMode == FlushModeType.COMMIT)
        return QueryFlushModes.FLUSH_FALSE;
    throw new ArgumentException(flushMode.toString(), null, null, false);
}