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.CloudManagerBean.java

public void deleteServer(Integer serverId) throws CloudManagerException {
    try {/*from   w  w  w  .j  av a 2  s. co m*/
        Server server = entityManager.find(Server.class, serverId);

        if (Server.OperationMode.NORMAL == server.getOperationMode()) {
            throw new CloudManagerException("Could not delete server " + server.getName()
                    + ". Server must be down or in maintenance mode. Current operating mode is: "
                    + server.getOperationMode().name());
        }

        // Delete any server list entries referencing this server
        failoverListManager.deleteServerListDetailsForServer(serverId);

        // Delete any agent references to this server
        Query query = entityManager.createNamedQuery(Agent.QUERY_REMOVE_SERVER_REFERENCE);
        query.setParameter("serverId", serverId);
        query.executeUpdate();

        // Then, delete the server
        query = entityManager.createNamedQuery(Server.QUERY_DELETE_BY_ID);
        query.setParameter("serverId", serverId);
        query.executeUpdate();

        entityManager.flush();
        entityManager.clear();

        log.info("Removed server " + server);

        // Now, request a cloud repartitioning due to the server removal
        partitionEventManager.cloudPartitionEventRequest(LookupUtil.getSubjectManager().getOverlord(),
                PartitionEventType.SERVER_DELETION, server.getName());

    } catch (Exception e) {
        throw new CloudManagerException("Could not delete server[id=" + serverId + "]: " + e.getMessage(), e);
    }
}

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

private void deleteAlertEventsOlderThan(Integer alertDefinitionId, long olderThan) {
    Query query = entityManager.createNamedQuery(AlertDampeningEvent.QUERY_DELETE_BY_TIMESTAMP);
    query.setParameter("alertDefinitionId", alertDefinitionId);
    query.setParameter("oldest", olderThan);

    int deletedCount = query.executeUpdate();

    if (deletedCount > 0) {
        log.debug("Deleted " + deletedCount + " stale AlertDampeningEvent" + ((deletedCount == 1) ? "" : "s")
                + " for AlertDefinition[id=" + alertDefinitionId + "]");
    }/*from   www . j a v a2  s .  co m*/
}

From source file:org.rhq.enterprise.server.cloud.instance.ServerManagerBean.java

private void clearAgentReferences(Server server) {
    Query query = entityManager.createNamedQuery(Agent.QUERY_REMOVE_SERVER_REFERENCE);
    query.setParameter("serverId", server.getId());
    int numRows = query.executeUpdate();
    if (numRows > 0) {
        log.info("Removed " + numRows + " obsolete agent reference(s) to server " + server.getName());
    }//from   w w w  .  j a v a 2s. c o m
}

From source file:eu.domibus.common.dao.MessageLogDao.java

private void setMessageStatus(final String messageId, MessageStatus messageStatus) {

    final Query query = this.em.createNamedQuery("MessageLogEntry.setMessageStatus");
    query.setParameter("MESSAGE_ID", messageId);
    query.setParameter("TIMESTAMP", new Date());
    query.setParameter("MESSAGE_STATUS", messageStatus);
    final int result = query.executeUpdate();
    if (result != 1) {
        this.em.getTransaction().setRollbackOnly();
        BasicDao.LOG.error("Could not set message " + messageId + " as " + messageStatus);
    }//from   www. j  a  va 2  s .  co  m
}

From source file:org.bitsofinfo.util.address.usps.ais.store.jpa.JPAStore.java

@Override
@Transactional(propagation = Propagation.REQUIRED)
public void purgeEntireStore() throws StoreException {

    List<Class<? extends USPSRecord>> clazzes = getClasses();
    for (Class<? extends USPSRecord> clazz : clazzes) {
        try {/*w  w  w  .  ja  v a2s.  co m*/
            TransactionStatus ts = this.transactionManager.getTransaction(null); // FIXME
            Query q = this.entityMgr.createQuery("delete from " + clazz.getSimpleName());
            int totalRemoved = q.executeUpdate();
            transactionManager.commit(ts);

        } catch (Exception e) {
            //throw new StoreException("Error in disabling and dropping all tables",e);
        }
    }

}

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

@Override
public void disableAccessToken(String accessTokenValue) {
    Query query = entityManager.createQuery(
            "update OrcidOauth2TokenDetail set tokenDisabled = TRUE where " + "tokenValue = :accessTokenValue");
    query.setParameter("accessTokenValue", accessTokenValue);
    int count = query.executeUpdate();
    if (count == 0) {
        LOGGER.debug("Cannot remove the refresh token {0}", accessTokenValue);
    }//from ww  w.j  a v a2s.  co  m
}

From source file:org.apereo.portal.portlet.dao.jpa.JpaPortletCookieDaoImpl.java

@Override
@PortalTransactional/*from  w  ww  .ja  va 2s.c o m*/
public void purgeExpiredCookies(int maxAge) {
    final DateTime now = DateTime.now();

    logger.debug("begin portlet cookie expiration");

    final EntityManager entityManager = this.getEntityManager();
    final Query deletePortletCookieQuery = entityManager.createQuery(this.deletePortletCookieQueryString);
    deletePortletCookieQuery.setParameter(this.nowParameter.getName(), now);
    final int deletedPortletCookies = deletePortletCookieQuery.executeUpdate();
    logger.debug("finished purging {} portlet cookies with expiration before {}", deletedPortletCookies, now);

    final TypedQuery<PortletCookieImpl> expiredByParentCookiesQuery = this
            .createQuery(findExpiredByParentPortletCookiesQuery);
    expiredByParentCookiesQuery.setParameter(this.nowParameter.getName(), now);
    final List<PortletCookieImpl> indirectlyExpiredCookies = expiredByParentCookiesQuery.getResultList();
    for (final PortletCookieImpl portletCookieImpl : indirectlyExpiredCookies) {
        entityManager.remove(portletCookieImpl);
    }
    logger.debug("finished purging {} portlet cookies with parent expiration before {}",
            indirectlyExpiredCookies.size(), now);

    logger.debug("begin portal cookie expiration");

    final Query deletePortalCookieQuery = entityManager.createQuery(this.deletePortalCookieQueryString);
    deletePortalCookieQuery.setParameter(this.nowParameter.getName(), now);
    final int deletedPortalCookies = deletePortalCookieQuery.executeUpdate();
    logger.debug("finished purging {} portal cookies with expiration before {}", deletedPortalCookies, now);

    final Query deleteEmptyPortalCookieQuery = entityManager
            .createQuery(this.deleteEmptyPortalCookieQueryString);
    //Add the maxAge to now and then subtract the emptyCookieMaxAge
    //For example (now + 1 year) - 1 day == the empty-cookie expiration date 
    final DateTime emptyExpiration = now.plusSeconds(maxAge).minusSeconds(emptyCookieMaxAge);
    deleteEmptyPortalCookieQuery.setParameter(this.nowParameter.getName(), emptyExpiration);
    final int deletedEmptyPortalCookies = deleteEmptyPortalCookieQuery.executeUpdate();
    logger.debug("finished purging {} empty portal cookies with expiration before {}",
            deletedEmptyPortalCookies, emptyExpiration);
}

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

@Transactional(readOnly = false)
public void deleteCategory(final TicketCategory category) {
    this.jpaTemplate.execute(new JpaCallback() {
        public Object doInJpa(EntityManager em) throws PersistenceException {
            if (category.hasChildNodes()) {
                deleteChildNodes(category);
                category.setRight(category.getLeft() + 1);
            }/*from  w ww . j  a  v a  2 s . c  om*/
            Query q1 = em.createNativeQuery(
                    "UPDATE ticket_category SET t_left=t_left-2 WHERE t_left>?1 AND t_left<?2");
            q1.setParameter(1, category.getRight());
            q1.setParameter(2, ((getNodeCount() * 2) + 1));
            q1.executeUpdate();

            Query q2 = em.createNativeQuery(
                    "UPDATE ticket_category SET t_right=t_right-2 WHERE t_right>=?1 AND t_right<=?2");
            q2.setParameter(1, category.getRight());
            q2.setParameter(2, (getNodeCount() * 2));
            q2.executeUpdate();

            Query q3 = em.createNativeQuery("DELETE FROM ticket_category WHERE category_id=?1");
            q3.setParameter(1, category.getTicketCategoryId());
            q3.executeUpdate();

            return null;
        }
    });
}

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

public void deletePendingInstances(String feedName, String clusterName) {
    EntityManager entityManager = getEntityManager();
    beginTransaction(entityManager);//from w  w w.  ja  v a 2 s . c o  m
    Query q = entityManager.createNamedQuery(PersistenceConstants.DELETE_ALL_INSTANCES_FOR_FEED);
    q.setParameter("feedName", feedName);
    q.setParameter("clusterName", clusterName);
    try {
        q.executeUpdate();
    } finally {
        commitAndCloseTransaction(entityManager);
    }
}

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

@Override
public void disableAccessTokenByRefreshToken(String refreshTokenValue) {
    Query query = entityManager.createQuery("update OrcidOauth2TokenDetail set tokenDisabled = TRUE where "
            + "refreshTokenValue = :refreshTokenValue");
    query.setParameter("refreshTokenValue", refreshTokenValue);
    int count = query.executeUpdate();
    if (count == 0) {
        LOGGER.debug("Cannot remove the refresh token {0}", refreshTokenValue);
    }/*from ww  w.j  a v a  2  s.  c  o m*/
}