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:in.bookmylab.jpa.JpaDAO.java

public User saveUser(User user, boolean savePassword) {
    EntityManager em = emf.createEntityManager();
    try {// w ww .ja va2  s  .  c o m
        user.password = Utils.hashPassword(user.password);
        em.getTransaction().begin();
        if (user.userId == null) {
            grantDefaultRoleToUser(em, user);
            em.persist(user);
        } else {
            user = em.merge(user);
            if (savePassword) {
                Query q = em.createNamedQuery("User.setPassword");
                q.setParameter("password", user.password);
                q.setParameter("userId", user.userId);
                q.executeUpdate();
            }
        }
        em.getTransaction().commit();
    } finally {
        em.clear();
        em.close();
    }
    return user;
}

From source file:mx.edu.ittepic.proyectofinal.ejbs.webservicesusers.java

public String updateUser(@PathParam("username") String userid, @PathParam("apikey") String apikey) {

    Message m = new Message();
    Users r = new Users();
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();/*  ww  w. j a v  a  2s . c om*/
    try {
        Query q = entity.createNamedQuery("Users.updateUser").

                setParameter("apikey", apikey).setParameter("userid", Integer.parseInt(userid));

        String idapi = userid;
        String md5 = DigestUtils.md5Hex(idapi);

        Query a = entity.createNamedQuery("Users.updateUserE").setParameter("apikey", userid)
                .setParameter("userid", Integer.parseInt(userid));

        if (q.executeUpdate() == 1 && a.executeUpdate() == 1) {
            m.setCode(200);
            m.setMsg("Se actualizo correctamente.");
            m.setDetail("OK");
        } else {
            m.setCode(404);
            m.setMsg("No se realizo la actualizacion");
            m.setDetail("");
        }

    } catch (IllegalStateException e) {
        m.setCode(404);
        m.setMsg("No se realizo la actualizacion");
        m.setDetail(e.toString());
    } catch (TransactionRequiredException e) {
        m.setCode(404);
        m.setMsg("No se realizo la actualizacion");
        m.setDetail(e.toString());
    } catch (QueryTimeoutException e) {
        m.setCode(404);
        m.setMsg("No se realizo la actualizacion");
        m.setDetail(e.toString());
    } catch (PersistenceException e) {
        m.setCode(404);
        m.setMsg("No se realizo la actualizacion");
        m.setDetail(e.toString());
    }
    return gson.toJson(m);
}

From source file:org.asqatasun.entity.dao.audit.ContentDAOImpl.java

/**
 * This native query is used to avoid multiple select before insert realized
 * by hibernate while persisting the content relationship relation.
 * @param ssp//from  w w w  . j  a  v a2  s .  c  om
 */
@Override
public void saveContentRelationShip(SSP ssp, Set<Long> relatedContentIdSet) {
    List<Long> relatedContentIds = findRelatedContentFromSsp(ssp);
    Set<Long> newRelatedContentIdSet = new HashSet<>();
    for (Long relatedContentId : relatedContentIdSet) {
        if (!relatedContentIds.contains(relatedContentId)) {
            newRelatedContentIdSet.add(relatedContentId);
        }
    }
    if (!newRelatedContentIdSet.isEmpty()) {
        StringBuilder queryValuesBuilder = new StringBuilder();
        for (Long relatedContentId : newRelatedContentIdSet) {
            queryValuesBuilder.append("(");
            queryValuesBuilder.append(ssp.getId());
            queryValuesBuilder.append(",");
            queryValuesBuilder.append(relatedContentId);
            queryValuesBuilder.append(")");
            queryValuesBuilder.append(",");
        }
        queryValuesBuilder.setCharAt(queryValuesBuilder.length() - 1, ';');
        Query query = entityManager.createNativeQuery(INSERT_QUERY + queryValuesBuilder.toString());
        try {
            query.executeUpdate();
            flushAndCloseEntityManager();
        } catch (ConstraintViolationException micve) {
            LOGGER.warn(micve.getMessage());
        } finally {
            flushAndCloseEntityManager();
        }
    }
}

From source file:org.kuali.mobility.push.dao.DeviceDaoImpl.java

@Transactional
public boolean removeAllDevicesWithUsername(String username) {
    boolean result = true;
    List<Device> devices = this.findDevicesByUsername(username);
    Query query = entityManager.createQuery("delete from Device where username = :username");
    query.setParameter("username", username);
    result = (query.executeUpdate() > 0) ? true : false;

    LOG.info("****** Found " + devices.size() + " devices with username " + username + " to delete.");
    return result;
}

From source file:org.opens.tanaguru.entity.dao.audit.ContentDAOImpl.java

/**
 * This native query is used to avoid multiple select before insert realized
 * by hibernate while persisting the content relationship relation.
 * @param ssp// w w w  . j a v  a 2 s . co m
 */
@Override
public void saveContentRelationShip(SSP ssp, Set<Long> relatedContentIdSet) {
    List<Long> relatedContentIds = findRelatedContentFromSsp(ssp);
    Set<Long> newRelatedContentIdSet = new HashSet<Long>();
    for (Long relatedContentId : relatedContentIdSet) {
        if (!relatedContentIds.contains(relatedContentId)) {
            newRelatedContentIdSet.add(relatedContentId);
        }
    }
    if (!newRelatedContentIdSet.isEmpty()) {
        StringBuilder queryValuesBuilder = new StringBuilder();
        for (Long relatedContentId : newRelatedContentIdSet) {
            queryValuesBuilder.append("(");
            queryValuesBuilder.append(ssp.getId());
            queryValuesBuilder.append(",");
            queryValuesBuilder.append(relatedContentId);
            queryValuesBuilder.append(")");
            queryValuesBuilder.append(",");
        }
        queryValuesBuilder.setCharAt(queryValuesBuilder.length() - 1, ';');
        Query query = entityManager.createNativeQuery(INSERT_QUERY + queryValuesBuilder.toString());
        try {
            query.executeUpdate();
            flushAndCloseEntityManager();
        } catch (ConstraintViolationException micve) {
            LOGGER.warn(micve.getMessage());
        } finally {
            flushAndCloseEntityManager();
        }
    }
}

From source file:net.kamhon.ieagle.dao.Jpa2Dao.java

public int bulkUpdate(String query, final Object... objectArray) {
    final String query2 = convertJpaPositionParams(query);
    Query q = em.createQuery(query2);
    for (int i = 0; i < objectArray.length; i++) {
        q.setParameter(i + 1, objectArray[i]);
    }/*from  w  w w .  jav  a 2s  .c om*/
    return q.executeUpdate();
}

From source file:org.asqatasun.entity.dao.audit.ContentDAOImpl.java

@Override
public void deleteRelatedContentFromContent(Content content) {
    Query query = entityManager.createNativeQuery(DELETE_RELATED_CONTENT_QUERY);
    query.setParameter("idContent", content.getId());
    try {//w w  w.  j  a  v a  2s . com
        query.executeUpdate();
    } catch (NoResultException nre) {
        // do nothing
    }
}

From source file:org.rhq.enterprise.server.measurement.MeasurementOOBManagerBean.java

public void removeOOBsForGroupAndDefinition(Subject subject, int resourceGroupId, int measurementDefinitionId) {
    Query query = entityManager.createNamedQuery(MeasurementOOB.DELETE_FOR_GROUP_AND_DEFINITION);
    query.setParameter("groupId", resourceGroupId);
    query.setParameter("definitionId", measurementDefinitionId);
    query.executeUpdate();
}

From source file:org.apache.rave.portal.repository.impl.JpaWidgetRepository.java

@Override
public int unassignWidgetOwner(String userId) {
    Query query = manager.createNamedQuery(JpaWidget.WIDGET_UNASSIGN_OWNER);
    query.setParameter(JpaWidget.PARAM_OWNER, userId);
    return query.executeUpdate();
}

From source file:net.kamhon.ieagle.dao.JpaDao.java

@Override
public int bulkUpdate(String query, final Object... objectArray) {
    final String query2 = convertJpaPositionParams(query);
    return getJpaTemplate().execute(new JpaCallback<Integer>() {
        @Override//from  www. j  a  v a2s  .  com
        public Integer doInJpa(EntityManager em) throws PersistenceException {
            Query q = em.createQuery(query2);
            for (int i = 0; i < objectArray.length; i++) {
                q.setParameter(i + 1, objectArray[i]);
            }
            return q.executeUpdate();
        }
    });
}