Example usage for javax.persistence Query executeUpdate

List of usage examples for javax.persistence Query executeUpdate

Introduction

In this page you can find the example usage for javax.persistence Query executeUpdate.

Prototype

int executeUpdate();

Source Link

Document

Execute an update or delete statement.

Usage

From source file:org.rhq.enterprise.server.alert.AlertDefinitionManagerBean.java

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void purgeInternals(int alertDefinitionId) {
    try {// w  ww . ja  v  a2 s.  c o  m
        Query alertDampeningEventPurgeQuery = entityManager
                .createNamedQuery(AlertDampeningEvent.QUERY_DELETE_BY_ALERT_DEFINITION_ID);
        Query unmatchedAlertConditionLogPurgeQuery = entityManager
                .createNamedQuery(AlertConditionLog.QUERY_DELETE_UNMATCHED_BY_ALERT_DEFINITION_ID);

        alertDampeningEventPurgeQuery.setParameter("alertDefinitionId", alertDefinitionId);
        unmatchedAlertConditionLogPurgeQuery.setParameter("alertDefinitionId", alertDefinitionId);

        int alertDampeningEventPurgeCount = alertDampeningEventPurgeQuery.executeUpdate();
        int unmatchedAlertConditionLogPurgeCount = unmatchedAlertConditionLogPurgeQuery.executeUpdate();

        if (LOG.isDebugEnabled()) {
            LOG.debug("Update to AlertDefinition[id=" + alertDefinitionId
                    + " caused a purge of internal, dampening constructs.");
            if (alertDampeningEventPurgeCount > 0) {
                LOG.debug("Removed " + alertDampeningEventPurgeCount + " AlertDampeningEvent"
                        + (alertDampeningEventPurgeCount == 1 ? "" : "s"));
            }
            if (unmatchedAlertConditionLogPurgeCount > 0) {
                LOG.debug("Removed " + unmatchedAlertConditionLogPurgeCount + " unmatched AlertConditionLog"
                        + (unmatchedAlertConditionLogPurgeCount == 1 ? "" : "s"));
            }
        }
    } catch (Throwable t) {
        LOG.debug("Could not purge internal alerting constructs for: " + alertDefinitionId, t);
    }
}

From source file:org.meveo.service.billing.impl.InvoiceService.java

@SuppressWarnings("unchecked")
public void deleteInvoice(Invoice invoice) {
    getEntityManager().createNamedQuery("RatedTransaction.deleteInvoice").setParameter("invoice", invoice)
            .executeUpdate();//from  w w w  . java  2s  .co m

    Query queryTrans = getEntityManager().createQuery("update " + RatedTransaction.class.getName()
            + " set invoice=null,invoiceAgregateF=null,invoiceAgregateR=null,invoiceAgregateT=null where invoice=:invoice");
    queryTrans.setParameter("invoice", invoice);
    queryTrans.executeUpdate();

    Query queryAgregate = getEntityManager()
            .createQuery("from " + InvoiceAgregate.class.getName() + " where invoice=:invoice");

    queryAgregate.setParameter("invoice", invoice);
    List<InvoiceAgregate> invoiceAgregates = (List<InvoiceAgregate>) queryAgregate.getResultList();
    for (InvoiceAgregate invoiceAgregate : invoiceAgregates) {
        getEntityManager().remove(invoiceAgregate);
    }
    getEntityManager().flush();

    Query queryInvoices = getEntityManager()
            .createQuery("delete from " + Invoice.class.getName() + " where id=:invoiceId");
    queryInvoices.setParameter("invoiceId", invoice.getId());
    queryInvoices.executeUpdate();
}

From source file:org.opencms.db.jpa.CmsSubscriptionDriver.java

/**
 * @see org.opencms.db.I_CmsSubscriptionDriver#deleteVisits(org.opencms.db.CmsDbContext, java.lang.String, org.opencms.db.CmsVisitEntryFilter)
 *///from  w  ww  . java 2 s.co  m
public void deleteVisits(CmsDbContext dbc, String poolName, CmsVisitEntryFilter filter)
        throws CmsDataAccessException {

    try {

        // compose statement 
        StringBuffer queryBuf = new StringBuffer(256);
        queryBuf.append(m_sqlManager.readQuery(C_VISIT_DELETE_ENTRIES));

        CmsPair<String, List<I_CmsQueryParameter>> conditionsAndParams = prepareVisitConditions(filter);
        queryBuf.append(conditionsAndParams.getFirst());
        if (LOG.isDebugEnabled()) {
            LOG.debug(queryBuf.toString());
        }
        Query q = m_sqlManager.createQueryFromJPQL(dbc, queryBuf.toString());
        List<I_CmsQueryParameter> params = conditionsAndParams.getSecond();
        for (int i = 0; i < params.size(); i++) {
            I_CmsQueryParameter param = conditionsAndParams.getSecond().get(i);
            param.insertIntoQuery(q, i + 1);
        }

        // execute
        q.executeUpdate();
    } catch (PersistenceException e) {
        throw new CmsDbSqlException(
                Messages.get().container(Messages.ERR_GENERIC_SQL_1, C_VISIT_DELETE_ENTRIES), e);
    }
}

From source file:com.doculibre.constellio.services.IndexFieldServicesImpl.java

@Override
public IndexField makeTransient(IndexField entity) {
    String[] facetIndexFieldProperties = { "facetField", "carrotTitleField", "carrotUrlField",
            "carrotSnippetField" };
    for (String facetIndexFieldProperty : facetIndexFieldProperties) {
        Query queryFacetField = getEntityManager()
                .createQuery("DELETE FROM CollectionFacet WHERE " + facetIndexFieldProperty + "=:indexField");
        queryFacetField.setParameter("indexField", entity);
        queryFacetField.executeUpdate();
    }//from   ww  w.  j a v  a 2s .  c o  m

    RecordCollectionServices collectionServices = ConstellioSpringUtils.getRecordCollectionServices();
    RecordCollection collection = entity.getRecordCollection();
    collection.getIndexFields().remove(entity);
    collectionServices.makePersistent(collection, true);
    return entity;
}

From source file:com.jada.admin.customAttribute.CustomAttributeGroupMaintAction.java

public ActionForward removeCustomAttributeDetails(ActionMapping mapping, ActionForm actionForm,
        HttpServletRequest request, HttpServletResponse response) throws Throwable {
    EntityManager em = JpaConnection.getInstance().getCurrentEntityManager();
    CustomAttributeGroupMaintActionForm form = (CustomAttributeGroupMaintActionForm) actionForm;
    AdminBean adminBean = getAdminBean(request);
    Site site = adminBean.getSite();//from   w  ww.  j  a v a 2  s.  co  m

    JSONEscapeObject jsonResult = new JSONEscapeObject();
    jsonResult.put("status", Constants.WEBSERVICE_STATUS_SUCCESS);

    Long customAttribGroupId = Format.getLong(form.getCustomAttribGroupId());
    CustomAttributeGroup customAttributeGroup = CustomAttributeGroupDAO.load(site.getSiteId(),
            customAttribGroupId);
    String customAttribDetailIds[] = form.getCustomAttribDetailIds();
    if (customAttribDetailIds != null) {
        for (int i = 0; i < customAttribDetailIds.length; i++) {
            CustomAttributeDetail customAttributeDetail = (CustomAttributeDetail) em
                    .find(CustomAttributeDetail.class, Format.getLong(customAttribDetailIds[i]));
            if (customAttributeDetail.getCustomAttribute()
                    .getCustomAttribTypeCode() == Constants.CUSTOM_ATTRIBUTE_TYPE_SKU_MAKEUP) {
                String sql = "select  count(*) " + "from     ItemAttributeDetail itemAttributeDetail "
                        + "where     itemAttributeDetail.customAttributeDetail = :customAttributeDetail "
                        + "and     itemAttributeDetail.item.itemTypeCd = :itemTypeCd";
                Query query = em.createQuery(sql);
                query.setParameter("customAttributeDetail", customAttributeDetail);
                query.setParameter("itemTypeCd", Constants.ITEM_TYPE_SKU);
                Long count = (Long) query.getSingleResult();
                if (count.intValue() > 0) {
                    jsonResult.put("status", Constants.WEBSERVICE_STATUS_FAILED);
                    jsonResult.put("reason", Constants.WEBSERVICE_REASON_INUSE);
                    streamWebService(response, jsonResult.toHtmlString());
                    return null;
                }
            }

            String sql = "delete " + "from   ItemAttributeDetail itemAttributeDetail "
                    + "where  itemAttributeDetail.customAttributeDetail = :customAttributeDetail ";
            Query query = em.createQuery(sql);
            query.setParameter("customAttributeDetail", customAttributeDetail);
            query.executeUpdate();

            customAttributeGroup.getCustomAttributeDetails().remove(customAttributeDetail);
            em.remove(customAttributeDetail);
        }
    }
    streamWebService(response, jsonResult.toHtmlString());
    return null;
}

From source file:com.tesora.dve.common.catalog.CatalogDAO.java

public void cleanupUserlandTemporaryTables(String serverName) {
    Query query = em.get().createQuery("delete from TemporaryTable where server = :name");
    query.setParameter("name", serverName);
    query.executeUpdate();
}

From source file:org.orcid.persistence.dao.impl.ProfileDaoImpl.java

@Override
@Transactional//from   w w w. j a  v  a 2  s.  c om
public void updateNames(String orcid, String givenNames, String familyName, String creditName,
        Visibility creditNameVisibility) {
    Query updateQuery = entityManager.createQuery(
            "update ProfileEntity set lastModified = now(), family_name = :familyName, given_names = :givenNames, credit_name = :creditName, names_visibility=:creditNameVisibility where orcid = :orcid");
    updateQuery.setParameter("orcid", orcid);
    updateQuery.setParameter("givenNames", givenNames);
    updateQuery.setParameter("familyName", familyName);
    updateQuery.setParameter("creditName", creditName);
    updateQuery.setParameter("creditNameVisibility",
            creditNameVisibility == null ? null : StringUtils.upperCase(creditNameVisibility.value()));
    updateQuery.executeUpdate();
}

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

public int executeUpdate(String query, Object... params) throws Exception {
    int res = 0;//from www .ja  v  a2 s  . co  m

    EntityManager em = getEntityManager();

    try {
        if (!globalTransactionOpen)
            em.getTransaction().begin();

        Query q = em.createQuery(query);

        if (params != null) {
            int i = 1;
            for (Object o : params) {
                q.setParameter(i, o);
                i++;
            }
        }

        res = q.executeUpdate();

        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();
        }
    }

    return res;
}

From source file:org.rhq.enterprise.server.content.RepoManagerBean.java

public void removeOwnershipOfSubject(int subjectId) {
    Query q = entityManager.createNamedQuery(Repo.QUERY_UPDATE_REMOVE_OWNER_FROM_REPOS_OWNED_BY_SUBJECT);
    q.setParameter("ownerId", subjectId);

    q.executeUpdate();
}