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.cloud.StatusManagerBean.java

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void updateByResource(Subject subject, int resourceId) {
    log.debug("About to mark status by resource");

    /* //from   w  w w  .j  a  va 2 s. co  m
     * the old alert definition is needed to know which caches to remove stale entries from; the updated / new
     * alert definition is needed to know which caches need to be reloaded to get the new conditions; by the time
     * this method is called, we only have the updated alert definition, thus it's not possible to intelligently
     * know which of the two caches to reload; so, we need to reload them both to be sure the system is consistent
     */
    markGlobalCache(); // use local references to execute in the same transaction

    Query updateAgentQuery = entityManager.createNamedQuery(Agent.QUERY_UPDATE_STATUS_BY_RESOURCE);
    updateAgentQuery.setParameter("resourceId", resourceId);
    int agentsUpdated = updateAgentQuery.executeUpdate();

    /*
     * this is informational debugging only - do NOT change the status bits here
     */
    if (log.isDebugEnabled()) {
        Agent agent = agentManager.getAgentByResourceId(LookupUtil.getSubjectManager().getOverlord(),
                resourceId);
        log.debug("Marking status, agent[id=" + agent.getId() + ", status=" + agent.getStatus()
                + "] for resource[id=" + resourceId + "]");

        log.debug("Agents updated: " + agentsUpdated);
    }
}

From source file:org.springframework.integration.jpa.core.DefaultJpaOperations.java

@Override
public int executeUpdateWithNativeQuery(String updateQuery, ParameterSource source) {
    Query query = entityManager.createNativeQuery(updateQuery);
    setParametersIfRequired(updateQuery, source, query);
    return query.executeUpdate();
}

From source file:org.medici.bia.dao.usermessage.UserMessageDAOJpaImpl.java

/**
 * {@inheritDoc}//  w w w .ja  v  a2  s  .co m
 */
@Override
public Integer removeMessages(User user, List<Integer> idElements) throws PersistenceException {
    StringBuilder query = new StringBuilder("DELETE FROM UserMessage WHERE user.account=:account AND (");
    for (int i = 0; i < idElements.size(); i++) {
        query.append("messageId=" + idElements.get(i));
        if (i != idElements.size() - 1) {
            query.append(" OR ");
        }
    }
    Query toQuery = getEntityManager().createQuery(query.toString() + ")");
    toQuery.setParameter("account", user.getAccount());

    return toQuery.executeUpdate();
}

From source file:org.apache.falcon.jdbc.BacklogMetricStore.java

public synchronized void deleteMetricInstance(String entityName, String cluster, Date nominalTime,
        EntityType entityType) {//from w w  w  . j  a v  a  2  s.c  o  m
    EntityManager entityManager = getEntityManager();
    beginTransaction(entityManager);
    Query q = entityManager.createNamedQuery(PersistenceConstants.DELETE_BACKLOG_METRIC_INSTANCE);
    q.setParameter("entityName", entityName);
    q.setParameter("clusterName", cluster);
    q.setParameter("nominalTime", nominalTime);
    q.setParameter("entityType", entityType.name());
    try {
        q.executeUpdate();
    } finally {
        commitAndCloseTransaction(entityManager);
    }
}

From source file:org.rhq.enterprise.server.cloud.StatusManagerBean.java

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void markGlobalCache() {
    Query updateServerQuery = entityManager.createNamedQuery(Server.QUERY_UPDATE_STATUS_BY_NAME);
    updateServerQuery.setParameter("identity", serverManager.getIdentity());
    int serversUpdated = updateServerQuery.executeUpdate();

    /*/*from   w  w w. j av  a 2 s . c  o  m*/
     * this is informational debugging only - do NOT change the status bits here
     */
    if (log.isDebugEnabled()) {
        Server server = serverManager.getServer();
        log.debug("Marking status, server[id=" + server.getId() + ", status=" + server.getStatus() + "]");

        log.debug("Servers updated: " + serversUpdated);
    }
}

From source file:eu.planets_project.tb.impl.persistency.ExperimentPersistencyImpl.java

public void removeMeasurementEvent(MeasurementEventImpl me) {
    // See above for removeMeasurement case and problems with this not working.
    //manager.remove( me );
    /* No longer done this way.
    for( MeasurementImpl m : me.getMeasurements() ) {
    this.removeMeasurement(m);/*www. j av a2 s  .c  om*/
    }
    */
    Query query = manager.createNativeQuery("DELETE FROM MeasurementEventImpl WHERE id=:id");
    query.setParameter("id", me.getId());
    query.executeUpdate();
}

From source file:de.berlios.jhelpdesk.dao.jpa.TicketCategoryDAOJpa.java

@Transactional(readOnly = false)
public void insertCategory(final TicketCategory category, final TicketCategory parent) {
    final long nodeCount = getNodeCount();
    this.jpaTemplate.execute(new JpaCallback() {
        public Object doInJpa(EntityManager em) throws PersistenceException {
            Query q1 = em.createNativeQuery(
                    "UPDATE ticket_category SET t_right=t_right+2 WHERE t_right>=? AND t_right<=?");
            q1.setParameter(1, parent.getRight());
            q1.setParameter(2, nodeCount * 2);
            q1.executeUpdate();

            Query q2 = em.createNativeQuery(
                    "UPDATE ticket_category SET t_left=t_left+2 WHERE t_left>? AND t_left<?");
            q2.setParameter(1, parent.getRight());
            q2.setParameter(2, (nodeCount + 1) * 2);
            q2.executeUpdate();/*from  w w w  . j a  v  a2 s .c om*/

            category.setLeft(parent.getRight());
            category.setRight(parent.getRight() + 1);
            category.setDepth(parent.getDepth() + 1);

            em.persist(category);
            return null;
        }
    });
}

From source file:de.micromata.genome.db.jpa.normsearch.NormalizedSearchServiceImpl.java

protected void deleteImpl(IEmgr<?> emgr, DbRecord entity) {

    NormSearchTable nsanot = entity.getClass().getAnnotation(NormSearchTable.class);
    Class<? extends NormSearchDO> nstable;
    if (nsanot != null) {
        nstable = nsanot.normSearchTable();
    } else if (entity instanceof NormSearchMasterTable) {
        nstable = ((NormSearchMasterTable) entity).getNormSearchTableClass();
    } else {//from  ww w  .  j  a va 2 s. c om
        return;
    }

    Query q = emgr.createUntypedQuery("delete from " + nstable.getName() + " d where d.parent = :parent",
            "parent", entity.getPk());
    q.executeUpdate();
}

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

@Override
@Transactional/*from  ww w. ja  va2  s .  c o m*/
public boolean updateOtherNamesVisibility(String orcid, Visibility visibility) {
    Query query = entityManager.createNativeQuery(
            "update profile set last_modified=now(), other_names_visibility=:other_names_visibility, indexing_status='PENDING' where orcid=:orcid");
    query.setParameter("other_names_visibility", StringUtils.upperCase(visibility.value()));
    query.setParameter("orcid", orcid);
    boolean result = query.executeUpdate() > 0 ? true : false;
    return result;

}

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

@Override
@Transactional//from  w w w  .java 2  s .c o m
@ExcludeFromProfileLastModifiedUpdate
public void removeByAuthenticationKeyOrTokenValueOrRefreshTokenValue(String authenticationKey,
        String tokenValue, String refreshTokenValue) {
    String or = " or ";
    Map<String, String> queryParams = new HashMap<String, String>();

    StringBuilder queryString = new StringBuilder("delete from OrcidOauth2TokenDetail where (");
    if (StringUtils.isNotBlank(authenticationKey)) {
        queryString.append("authenticationKey = :authenticationKey" + or);
        queryParams.put("authenticationKey", authenticationKey);
    }
    if (StringUtils.isNotBlank(tokenValue)) {
        queryString.append("tokenValue = :tokenValue" + or);
        queryParams.put("tokenValue", tokenValue);
    }
    if (StringUtils.isNotBlank(refreshTokenValue)) {
        queryString.append("refreshTokenValue = :refreshTokenValue" + or);
        queryParams.put("refreshTokenValue", refreshTokenValue);
    }

    if (!queryParams.isEmpty()) {
        queryString.replace(queryString.length() - or.length(), queryString.length(), ")");
        Query query = entityManager.createQuery(queryString.toString());
        for (String key : queryParams.keySet()) {
            query.setParameter(key, queryParams.get(key));
        }
        int i = query.executeUpdate();
        LOGGER.debug(i + " tokens deleted as a result of the parameters {}", queryParams);
    } else {
        LOGGER.info("Attempted to delete tokens with no parameters");
    }

}