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

@RequiredPermissions({ @RequiredPermission(Permission.MANAGE_SETTINGS),
        @RequiredPermission(Permission.MANAGE_INVENTORY) })
public void deleteServer(Subject subject, Integer serverId) throws TopologyManagerException {
    try {/* w  ww  .  j  a v a 2 s .  c o  m*/
        Server server = entityManager.find(Server.class, serverId);

        if (Server.OperationMode.NORMAL == server.getOperationMode()) {
            throw new TopologyManagerException("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 TopologyManagerException("Could not delete server[id=" + serverId + "]: " + e.getMessage(),
                e);
    }
}

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

@RequiredPermission(Permission.MANAGE_INVENTORY)
public void addAgentsToGroup(Subject subject, int affinityGroupId, Integer[] agentIds) {
    List<Integer> agentIdsList = Arrays.asList(agentIds);

    AffinityGroup group = entityManager.find(AffinityGroup.class, affinityGroupId);

    Query query = entityManager.createNamedQuery(AffinityGroup.QUERY_UPDATE_ADD_AGENTS);
    query.setParameter("affinityGroup", group);
    query.setParameter("agentIds", agentIdsList);

    query.executeUpdate();

    // Audit each changed affinity group assignment (is this too verbose?)
    String auditString = group.getName() + " <-- ";
    for (Integer agentId : agentIdsList) {
        Agent agent = entityManager.find(Agent.class, agentId);
        partitionEventManager.auditPartitionEvent(subject, PartitionEventType.AGENT_AFFINITY_GROUP_ASSIGN,
                auditString + agent.getName());

    }//from   ww w.  j  a v a2s  .c o m
    // Now, request a cloud repartitioning due to the affinity group changes
    partitionEventManager.cloudPartitionEventRequest(subject, PartitionEventType.AFFINITY_GROUP_CHANGE,
            group.getName());
}

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

public void deletePendingInstance(String feedName, String clusterName, Date nominalTime) {
    EntityManager entityManager = getEntityManager();
    beginTransaction(entityManager);/* w w  w.  j a  va  2s  .  c  o m*/
    Query q = entityManager.createNamedQuery(PersistenceConstants.DELETE_PENDING_NOMINAL_INSTANCES);
    q.setParameter("feedName", feedName);
    q.setParameter("clusterName", clusterName);
    q.setParameter("nominalTime", nominalTime);
    try {
        q.executeUpdate();
    } finally {
        commitAndCloseTransaction(entityManager);
    }
}

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

@RequiredPermission(Permission.MANAGE_INVENTORY)
public void addServersToGroup(Subject subject, int affinityGroupId, Integer[] serverIds) {
    List<Integer> serverIdsList = Arrays.asList(serverIds);

    AffinityGroup group = entityManager.find(AffinityGroup.class, affinityGroupId);

    Query query = entityManager.createNamedQuery(AffinityGroup.QUERY_UPDATE_ADD_SERVERS);
    query.setParameter("affinityGroup", group);
    query.setParameter("serverIds", serverIdsList);

    query.executeUpdate();

    // Audit each changed affinity group assignment (is this too verbose?)
    String auditString = group.getName() + " <-- ";
    for (Integer serverId : serverIdsList) {
        Server server = entityManager.find(Server.class, serverId);
        partitionEventManager.auditPartitionEvent(subject, PartitionEventType.SERVER_AFFINITY_GROUP_ASSIGN,
                auditString + server.getName());

    }//from w  w  w . ja v a2 s .  com
    // Now, request a cloud repartitioning due to the affinity group changes
    partitionEventManager.cloudPartitionEventRequest(subject, PartitionEventType.AFFINITY_GROUP_CHANGE,
            group.getName());
}

From source file:org.rhq.enterprise.server.content.test.ContentUIManagerBeanEligiblePackagesTest.java

private void tearDownTestEnvironment() throws Exception {
    getTransactionManager().begin();/*from   www. ja v a  2 s  .  c  om*/
    EntityManager em = getEntityManager();

    try {
        try {
            Query q = em.createNamedQuery(RepoPackageVersion.DELETE_BY_REPO_ID);
            q.setParameter("repoId", repo1.getId());
            q.executeUpdate();

            q = em.createNamedQuery(ResourceRepo.DELETE_BY_RESOURCE_ID);
            q.setParameter("resourceId", resource.getId());
            q.executeUpdate();

            getTransactionManager().commit();
        } catch (Exception e) {
            e.printStackTrace();
            getTransactionManager().rollback();
            throw e;
        }
    } finally {
        em.close();
    }

    getTransactionManager().begin();
    em = getEntityManager();

    try {
        try {
            resource = em.find(Resource.class, resource.getId());
            for (InstalledPackage ip : resource.getInstalledPackages()) {
                em.remove(ip);
            }

            package1 = em.find(Package.class, package1.getId());
            em.remove(package1);

            package2 = em.find(Package.class, package2.getId());
            em.remove(package2);

            package3 = em.find(Package.class, package3.getId());
            em.remove(package3);

            package4 = em.find(Package.class, package4.getId());
            em.remove(package4);

            packageType1 = em.find(PackageType.class, packageType1.getId());
            em.remove(packageType1);

            ResourceTreeHelper.deleteResource(em, resource);

            repo1 = em.find(Repo.class, repo1.getId());
            em.remove(repo1);

            repo2 = em.find(Repo.class, repo2.getId());
            em.remove(repo2);

            productVersion1 = em.find(ProductVersion.class, productVersion1.getId());
            em.remove(productVersion1);

            productVersion2 = em.find(ProductVersion.class, productVersion2.getId());
            em.remove(productVersion2);

            resourceType = em.find(ResourceType.class, resourceType.getId());
            em.remove(resourceType);

            getTransactionManager().commit();
        } catch (Exception e) {
            e.printStackTrace();
            getTransactionManager().rollback();
            throw e;
        }
    } finally {
        em.close();
    }
}

From source file:ch.puzzle.itc.mobiliar.business.resourcegroup.control.ResourceTypeDomainService.java

public int updateResourceTypeRelationName(Integer relationId, String identifier) {
    Query q = entityManager
            .createQuery("update ResourceRelationTypeEntity r set identifier = :identifier where id = :id");
    q.setParameter("id", relationId);
    q.setParameter("identifier", identifier);
    return q.executeUpdate();
}

From source file:org.messic.server.datamodel.jpaimpl.DAOJPAAlbum.java

@Override
@Transactional/* w w  w . j  a  v  a  2 s.co  m*/
public void setAlbumCover(long resourceSid, long albumSid, long userSid) {
    Query query = entityManager.createQuery(
            "UPDATE MDOArtwork a set a.cover = false WHERE a.owner.sid= :userSid AND a.album.sid= :albumSid");
    query.setParameter("userSid", userSid);
    query.setParameter("albumSid", albumSid);
    query.executeUpdate();

    query = entityManager.createQuery(
            "UPDATE MDOArtwork a set a.cover = true WHERE a.owner.sid= :userSid AND a.album.sid= :albumSid AND a.sid = :resourceSid");
    query.setParameter("resourceSid", resourceSid);
    query.setParameter("albumSid", albumSid);
    query.setParameter("userSid", userSid);
    query.executeUpdate();
}

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

@Transactional
public UserBean updateAutorization(UserBean ub, RequestContext ctx) {

    Query queryRemoveAuthorities = em.createNamedQuery("removeAuthorities");
    queryRemoveAuthorities.setParameter("username", ub.getUsername());
    queryRemoveAuthorities.executeUpdate();

    UserEntity ue = ub.getUserEntity();//from   w w w. j a va 2s .  c  o m
    UserEntity ue0 = new UserEntity();
    ue0 = em.find(UserEntity.class, ub.getId());
    ue0.setEnabled(ue.getEnabled());
    // grab the auths
    List<AuthEntity> ael = new ArrayList<AuthEntity>();
    for (AuthBean ab : ub.getAuthSet()) {
        AuthEntity ae = new AuthEntity();
        ae.setAuthority(ab.getAuthority());
        ae.setUsername(ab.getUsername());
        ae.setUser_id(ub.getId());
        ael.add(ae);
    }
    // clear out auths first
    ue0.setAuthSet(ael);
    em.merge(ue0);
    em.flush();
    // Add message to be displayed on UI
    ctx.getMessageContext()
            .addMessage(new MessageBuilder().info().defaultText("The role was added successfuly.").build());
    return new UserBean(ue0);
}

From source file:org.debux.webmotion.jpa.GenericDAO.java

/**
 * Execute a query named as write in entity.
 * //  w w w  . j av  a 2 s .co m
 * @param name query name
 * @param parameters parameters
 * @return the number of entities updated or deleted 
 */
public int exec(String name, Parameters parameters) {
    Query query = manager.createNamedQuery(name);
    extract(query, parameters);
    int executeUpdate = query.executeUpdate();
    return executeUpdate;
}

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

private void clear() {
    Query query = entityManager.createNamedQuery(FailoverListDetails.QUERY_TRUNCATE);
    query.executeUpdate();
    query = entityManager.createNamedQuery(FailoverList.QUERY_TRUNCATE);
    query.executeUpdate();//www.j  ava2s.c o m
}