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.orcid.persistence.dao.impl.ProfileDaoImpl.java

@Override
@Transactional//from   ww  w  .j  a  v a 2  s.  co  m
@ExcludeFromProfileLastModifiedUpdate
public void updateLastModifiedDateAndIndexingStatus(String orcid) {
    Query updateQuery = entityManager.createQuery(
            "update ProfileEntity set lastModified = now(), indexingStatus = 'PENDING' where orcid = :orcid");
    updateQuery.setParameter("orcid", orcid);
    updateQuery.executeUpdate();
}

From source file:de.tudarmstadt.ukp.clarin.webanno.webapp.migration.FixCoreferenceFeatures.java

private void doMigration() {
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setName("migrationRoot");
    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

    TransactionStatus status = null;/*from   ww  w.  j  av a 2 s  .c  o m*/
    try {
        status = txManager.getTransaction(def);
        Query q = entityManager.createQuery("UPDATE AnnotationFeature \n" + "SET type = :fixedType \n"
                + "WHERE type = :oldType \n" + "AND name in (:featureNames)");

        // This condition cannot be applied: 
        //   "AND layer.type = :layerType"
        //   q.setParameter("layerType", CHAIN_TYPE);
        // http://stackoverflow.com/questions/16506759/hql-is-generating-incomplete-cross-join-on-executeupdate
        // However, the risk that the migration upgrades the wrong featuers is still very low
        // even without this additional condition

        q.setParameter("featureNames", Arrays.asList(COREFERENCE_RELATION_FEATURE, COREFERENCE_TYPE_FEATURE));
        q.setParameter("oldType", "de.tudarmstadt.ukp.dkpro.core.api.coref.type.Coreference");
        q.setParameter("fixedType", CAS.TYPE_NAME_STRING);
        int changed = q.executeUpdate();
        if (changed > 0) {
            log.info("DATABASE UPGRADE PERFORMED: [" + changed + "] coref chain features had their type fixed");
        }
        txManager.commit(status);
    } finally {
        if (status != null && !status.isCompleted()) {
            txManager.rollback(status);
        }
    }
}

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

@Override
public Thesaurus makeTransient(Thesaurus thesaurus) {
    //        for (SkosConcept topConcept : thesaurus.getTopConcepts()) {
    //           makeTransient(topConcept, false);
    //      }/*w w w  .  j a v  a2 s.co  m*/
    //        super.makeTransient(thesaurus);

    //        String sqlRecordTag = "DELETE FROM RecordTag WHERE skosConcept_id IN (SELECT id FROM SkosConcept WHERE thesaurus_id=?)";
    //        Query sqlQueryRecordTag = getEntityManager().createNativeQuery(sqlRecordTag);
    //        sqlQueryRecordTag.setParameter(1, thesaurus.getId());
    //        sqlQueryRecordTag.executeUpdate();

    String sqlAltLabelValues = "DELETE FROM SkosConceptAltLabel_Values WHERE skosConceptAltLabel_id IN (SELECT id FROM SkosConceptAltLabel WHERE skosConcept_id IN (SELECT id FROM SkosConcept WHERE thesaurus_id=?))";
    Query sqlQueryAltLabelValues = getEntityManager().createNativeQuery(sqlAltLabelValues);
    sqlQueryAltLabelValues.setParameter(1, thesaurus.getId());
    sqlQueryAltLabelValues.executeUpdate();

    String sqlAltLabel = "DELETE FROM SkosConceptAltLabel WHERE skosConcept_id IN (SELECT id FROM SkosConcept WHERE thesaurus_id=?)";
    Query sqlQueryAltLabel = getEntityManager().createNativeQuery(sqlAltLabel);
    sqlQueryAltLabel.setParameter(1, thesaurus.getId());
    sqlQueryAltLabel.executeUpdate();

    String sqlLabels = "DELETE FROM SkosConcept_Labels WHERE skosConcept_id IN (SELECT id FROM SkosConcept WHERE thesaurus_id=?)";
    Query sqlQueryLabels = getEntityManager().createNativeQuery(sqlLabels);
    sqlQueryLabels.setParameter(1, thesaurus.getId());
    sqlQueryLabels.executeUpdate();

    String sqlRelationsSource = "DELETE FROM SkosConcept_Relations WHERE sourceSkosConcept_id IN (SELECT id FROM SkosConcept WHERE thesaurus_id=?)";
    Query sqlQueryRelationsSource = getEntityManager().createNativeQuery(sqlRelationsSource);
    sqlQueryRelationsSource.setParameter(1, thesaurus.getId());
    sqlQueryRelationsSource.executeUpdate();

    String sqlRelationsRelated = "DELETE FROM SkosConcept_Relations WHERE relatedSkosConcept_id IN (SELECT id FROM SkosConcept WHERE thesaurus_id=?)";
    Query sqlQueryRelationsRelated = getEntityManager().createNativeQuery(sqlRelationsRelated);
    sqlQueryRelationsRelated.setParameter(1, thesaurus.getId());
    sqlQueryRelationsRelated.executeUpdate();

    String sqlNarrower = "DELETE FROM SkosConcept_Narrower WHERE narrowerSkosConcept_id IN (SELECT id FROM SkosConcept WHERE thesaurus_id=?)";
    Query sqlQueryNarrower = getEntityManager().createNativeQuery(sqlNarrower);
    sqlQueryNarrower.setParameter(1, thesaurus.getId());
    sqlQueryNarrower.executeUpdate();

    String sqlBroader = "DELETE FROM SkosConcept_Narrower WHERE broaderSkosConcept_id IN (SELECT id FROM SkosConcept WHERE thesaurus_id=?)";
    Query sqlQueryBroader = getEntityManager().createNativeQuery(sqlBroader);
    sqlQueryBroader.setParameter(1, thesaurus.getId());
    sqlQueryBroader.executeUpdate();

    String sqlSkosConcept = "DELETE FROM SkosConcept WHERE thesaurus_id=?";
    Query sqlQuerySkosConcept = getEntityManager().createNativeQuery(sqlSkosConcept);
    sqlQuerySkosConcept.setParameter(1, thesaurus.getId());
    sqlQuerySkosConcept.executeUpdate();

    String sqlThesaurus = "DELETE FROM Thesaurus WHERE id=?";
    Query sqlQueryThesaurus = getEntityManager().createNativeQuery(sqlThesaurus);
    sqlQueryThesaurus.setParameter(1, thesaurus.getId());
    sqlQueryThesaurus.executeUpdate();

    // Will synchronize objects with database
    getEntityManager().flush();

    SkosIndexHelper indexHelper = ConstellioSpringUtils.getSkosIndexHelper();
    indexHelper.delete(thesaurus);

    return thesaurus;
}

From source file:org.rhq.enterprise.server.event.EventManagerBean.java

public int purgeEventsForContext(Subject subject, EntityContext context) {

    if (context.type == EntityContext.Type.Resource) {
        if (authorizationManager.hasResourcePermission(subject, Permission.MANAGE_EVENTS,
                context.resourceId) == false) {
            throw new PermissionException("User [" + subject.getName()
                    + "] does not have permissions to purge events for resource[id=" + context.resourceId
                    + "]");
        }/*from  ww  w .j  av  a 2s.co  m*/
    } else if (context.type == EntityContext.Type.ResourceGroup) {
        if (authorizationManager.hasGroupPermission(subject, Permission.MANAGE_EVENTS,
                context.groupId) == false) {
            throw new PermissionException("User [" + subject.getName()
                    + "] does not have permissions to purge events for resourceGroup[id=" + context.groupId
                    + "]");
        }
    } else {
        throw new IllegalArgumentException(context.getUnknownContextMessage());
    }

    Query purgeQuery = null;
    if (context.type == EntityContext.Type.Resource) {
        purgeQuery = entityManager.createNamedQuery(Event.DELETE_ALL_BY_RESOURCE);
        purgeQuery.setParameter("resourceId", context.resourceId);
    } else if (context.type == EntityContext.Type.ResourceGroup) {
        purgeQuery = entityManager.createNamedQuery(Event.DELETE_ALL_BY_RESOURCE_GROUP);
        purgeQuery.setParameter("groupId", context.groupId);
    }

    int deletedCount = purgeQuery.executeUpdate();

    return deletedCount;
}

From source file:edu.kit.dama.mdm.core.jpa.MetaDataManagerJpa.java

@Override
public final Integer performUpdate(String queryString, Object[] pParameters)
        throws UnauthorizedAccessAttemptException {
    Integer result = null;/*from w ww  .  j a v  a  2  s  .c  om*/
    EntityTransaction transaction = entityManager.getTransaction();
    try {
        Query q = entityManager.createQuery(queryString);
        applyProperties(q);
        if (pParameters != null && pParameters.length != 0) {
            for (int i = 0; i < pParameters.length; i++) {
                q.setParameter(i + 1, pParameters[i]);
            }
        }
        transaction.begin();
        result = q.executeUpdate();
    } catch (RuntimeException re) {
        LOGGER.warn("Failed to obtain generic update result", re);
    } finally {
        finalizeEntityManagerAccess("update with plain SQL '" + queryString + "'", transaction, Object.class);
    }
    return result;
}

From source file:org.traccar.web.server.model.DataServiceImpl.java

@Override
public Device removeDevice(Device device) {
    EntityManager entityManager = getSessionEntityManager();
    synchronized (entityManager) {
        User user = getSessionUser();//from   ww w  .j a  va  2  s  .  com
        entityManager.getTransaction().begin();
        try {
            device = entityManager.merge(device);
            user.getDevices().remove(device);
            device.setLatestPosition(null);
            entityManager.flush();
            Query query = entityManager.createQuery("DELETE FROM Position x WHERE x.device = :device");
            query.setParameter("device", device);
            query.executeUpdate();
            entityManager.remove(device);
            entityManager.getTransaction().commit();
            return device;
        } catch (RuntimeException e) {
            entityManager.getTransaction().rollback();
            throw e;
        }
    }
}

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

@Override
@Transactional/*from w  w w  . j  a va  2 s.  c  om*/
public void updateLocale(String orcid, Locale locale) {
    Query updateQuery = entityManager.createQuery(
            "update ProfileEntity set lastModified = now(), locale = :locale where orcid = :orcid");
    updateQuery.setParameter("orcid", orcid);
    updateQuery.setParameter("locale", locale);
    updateQuery.executeUpdate();

}

From source file:org.eclipse.smila.connectivity.deltaindexing.jpa.impl.DeltaIndexingManagerImpl.java

/**
 * {@inheritDoc}/*from   ww  w.  ja  va 2s  . c  o  m*/
 * 
 * @see org.eclipse.smila.connectivity.deltaindexing.DeltaIndexingManager#unlockDatasources()
 */
@Override
public void unlockDatasources() throws DeltaIndexingException {
    _lock.readLock().lock();
    try {
        final EntityManager em = createEntityManager();
        final EntityTransaction transaction = em.getTransaction();
        try {
            transaction.begin();
            final Query query = em.createNamedQuery(DataSourceDao.NAMED_QUERY_KILL_ALL_SESSIONS);
            query.executeUpdate();
            transaction.commit();
            if (_log.isInfoEnabled()) {
                _log.info("removed all delta indexing sessions and unlocked all data sources");
            }
        } catch (final Exception e) {
            if (transaction.isActive()) {
                transaction.rollback();
            }
            throw new DeltaIndexingException("error unlocking delta indexing data sources", e);
        } finally {
            closeEntityManager(em);
        }
    } finally {
        _lock.readLock().unlock();
    }
}

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

private void updateWebhookProfileLastUpdate(String orcid) {
    Query query = entityManager.createNativeQuery(
            "update webhook set profile_last_modified = (select last_modified from profile where orcid = :orcid ) "
                    + "where orcid = :orcid ");
    query.setParameter("orcid", orcid);
    query.executeUpdate();
}

From source file:org.eclipse.smila.connectivity.deltaindexing.jpa.impl.DeltaIndexingManagerImpl.java

/**
 * {@inheritDoc}/*from  ww w.  j  a  va 2 s  .c o  m*/
 * 
 * @see org.eclipse.smila.connectivity.deltaindexing.DeltaIndexingManager#clear()
 */
@Override
public void clear() throws DeltaIndexingException {
    _lock.readLock().lock();
    try {
        final EntityManager em = createEntityManager();
        final EntityTransaction transaction = em.getTransaction();
        try {
            transaction.begin();
            // delete delta indexing entries
            final Query diQuery = em.createNamedQuery(DeltaIndexingDao.NAMED_QUERY_DELETE_ALL);
            diQuery.executeUpdate();
            // delete source
            final Query dsQuery = em.createNamedQuery(DataSourceDao.NAMED_QUERY_DELETE_SOURCES);
            dsQuery.executeUpdate();

            transaction.commit();
            if (_log.isInfoEnabled()) {
                _log.info("cleared delta indexing");
            }
        } catch (final Exception e) {
            if (transaction.isActive()) {
                transaction.rollback();
            }
            throw new DeltaIndexingException("error clearing delta indexing", e);
        } finally {
            closeEntityManager(em);
        }
    } finally {
        _lock.readLock().unlock();
    }

}