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:es.ucm.fdi.dalgs.activity.repository.ActivityRepository.java

public boolean deleteActivitiesFromGroups(Collection<Group> groups) {

    try {/*from w ww .  j  a v  a2 s .  c om*/
        Query query = em.createQuery("UPDATE Activity a SET a.isDeleted = true where a.group in ?1");

        query.setParameter(1, groups);
        query.executeUpdate();

    } catch (Exception e) {
        logger.error(e.getMessage());

        return false;
    }
    return true;
}

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

@SuppressWarnings("unchecked")
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public List<Integer> getAndClearAgentsWithStatusForServer(String serverName) {
    Query selectQuery = entityManager.createNamedQuery(Agent.QUERY_FIND_ALL_WITH_STATUS_BY_SERVER);
    selectQuery.setParameter("serverName", serverName);
    List<Integer> agentIds = selectQuery.getResultList();

    if (agentIds.size() > 0) {
        /* //from   ww  w .ja  v a 2 s.co  m
         * note: not worried about size of the in clause, because the number of
         * agents per server will be reasonable, say, 50-150
         */
        Query updateQuery = entityManager.createNamedQuery(Agent.QUERY_UPDATE_CLEAR_STATUS_BY_IDS);
        updateQuery.setParameter("agentIds", agentIds);
        updateQuery.executeUpdate();
    }

    return agentIds;
}

From source file:es.ucm.fdi.dalgs.activity.repository.ActivityRepository.java

public boolean deleteActivitiesFromCourses(Collection<Course> courses) {

    try {/*from  ww w. ja v a 2 s .  co m*/
        Query query = em.createQuery("UPDATE Activity a SET a.isDeleted = true where a.course in ?1");

        query.setParameter(1, courses);
        query.executeUpdate();

    } catch (Exception e) {
        logger.error(e.getMessage());

        return false;
    }
    return true;
}

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

public void deleteMonitoringFeed(String feedName) {
    EntityManager entityManager = getEntityManager();
    beginTransaction(entityManager);//www.j a  v  a2 s .  c  o m
    Query q = entityManager.createNamedQuery(PersistenceConstants.DELETE_MONITORED_INSTANCES);
    q.setParameter("feedName", feedName);
    try {
        q.executeUpdate();
    } finally {
        commitAndCloseTransaction(entityManager);
    }
}

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

@Transactional(readOnly = false) // TODO: to chyba nie powinno byc oddzielnie?
private void deleteChildNodes(final TicketCategory category) {
    final long nodeCount = getNodeCount();
    final long subtreeNodeCount = (category.getRight() - category.getLeft()) / 2;

    this.jpaTemplate.execute(new JpaCallback() {
        public Object doInJpa(EntityManager em) throws PersistenceException {
            Query q = em.createQuery("DELETE FROM TicketCategory c WHERE c.left > ?1 AND c.right < ?2");
            q.setParameter(1, category.getLeft());
            q.setParameter(2, category.getRight());
            q.executeUpdate();
            Query q2 = em.createQuery(
                    "UPDATE TicketCategory c SET c.left = c.left - ?1 " + "WHERE c.left > ?2 AND c.left < ?3");
            q2.setParameter(1, subtreeNodeCount * 2);
            q2.setParameter(2, category.getLeft());
            q2.setParameter(3, nodeCount * 2);
            q2.executeUpdate();//from   www .j a  v  a 2s  .c  o  m

            Query q3 = em.createQuery("UPDATE TicketCategory c SET c.right = c.right - ?1 "
                    + "WHERE c.right >= ?2 AND c.right <= ?3");
            q3.setParameter(1, subtreeNodeCount * 2);
            q3.setParameter(2, category.getRight());
            q3.setParameter(3, nodeCount * 2);
            q3.executeUpdate();

            return null;
        }
    });
}

From source file:org.openmeetings.app.data.user.dao.UserContactsDaoImpl.java

public Integer deleteAllUserContacts(Long ownerId) {
    try {/* w ww  .j  av  a  2  s  . c  o  m*/

        String hql = "delete from UserContacts u where u.owner.user_id = :ownerId";

        Query query = em.createQuery(hql);
        query.setParameter("ownerId", ownerId);
        int rowCount = query.executeUpdate();

        return rowCount;
    } catch (Exception e) {
        log.error("[deleteAllUserContacts]", e);
    }
    return null;
}

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

@Override
@ExcludeFromProfileLastModifiedUpdate/*from w ww . java2s  .  c om*/
public void removeByTokenValue(String tokenValue) {
    Assert.hasText(tokenValue, "Attempt to retrieve a OrcidOauth2TokenDetail with a null or empty token value");
    Query query = entityManager
            .createQuery("delete from OrcidOauth2TokenDetail where tokenValue = " + ":tokenValue");
    query.setParameter("tokenValue", tokenValue);
    int i = query.executeUpdate();
    if (i == 0) {
        LOGGER.info("Attempted to delete access token {0} but it was not present in the database", tokenValue);
    }
}

From source file:com.exp.tracker.services.impl.JpaSettlementService.java

@Transactional
public int deleteSettlement(Long sid, RequestContext ctx) {
    // 0 return code is good. 1 is bad
    int result = 0;
    try {//from   w  w w . j  ava2  s  .co m
        // delete reports
        Query queryDeleteReportsForSid = em.createNamedQuery("deleteReportsForSid");
        queryDeleteReportsForSid.setParameter("sid", sid);
        queryDeleteReportsForSid.executeUpdate();
        // reports deleted
        SettlementEntity se = em.find(SettlementEntity.class, sid);
        em.remove(se);
    } catch (Exception e) {
        result = 1;
        ctx.getMessageContext()
                .addMessage(new MessageBuilder().error()
                        .defaultText(
                                "Failed to delete settlement. Unsettled expenses might be preventing deletion.")
                        .build());
        logger.error("Error occured while deleting settlement " + e.getMessage(), e);
    }
    ctx.getMessageContext()
            .addMessage(new MessageBuilder().info().defaultText("Settlement deleted succesfuly.").build());
    return result;
}

From source file:org.kuali.continuity.admin.dao.jpa.UserLoginDAOImpl.java

@Override
public void changePassword(final int userId, final String password) {
    // get hash//ww w .  j a  v  a 2 s .co  m
    final String passwordHash = this.getCaseInsensitivePasswordHash(password);

    // query
    Query query = this.getEntityManager().createNativeQuery(APPROVE_USER_QUERY);
    query.setParameter("password", passwordHash);
    query.setParameter("userId", userId);
    query.setParameter("isApproved", '1');

    // update
    query.executeUpdate();
}

From source file:co.sip.dmesmobile.bs.ScStopDao.java

@Override
@Transactional//from w w w .  jav a2s .c  o  m
public int setMachineState(Long idMachine, int idState) throws Exception {
    entityManager = Factory.getEntityManagerFactory().createEntityManager();
    int result = -1;
    try {
        ScMachine machine = new ScMachine(idMachine);
        entityManager.getTransaction().begin();
        Query query = entityManager.createNativeQuery(
                "UPDATE dmes.sc_machine SET id_state = " + idState + "WHERE id_machine = " + idMachine);
        result = query.executeUpdate();
        entityManager.getTransaction().commit();

    } catch (Exception e) {
        log.error("Error intentando actualizar la mquina del paro", e);
        entityManager.getTransaction().rollback();
        throw e;
    }
    return result;
}