Example usage for org.hibernate Query setMaxResults

List of usage examples for org.hibernate Query setMaxResults

Introduction

In this page you can find the example usage for org.hibernate Query setMaxResults.

Prototype

@Override
    Query<R> setMaxResults(int maxResult);

Source Link

Usage

From source file:com.dell.asm.asmcore.asmmanager.db.DiscoveryResultDAO.java

License:Open Source License

/**
 * create or Update Discovery Result.//from   ww w  .j  a v a  2s . c  o  m
 * @param newResult the result to update.
 */

public void createOrUpdateDiscoveryResult(DiscoveryResultEntity newResult) throws AsmManagerCheckedException {

    Session session = null;
    Transaction tx = null;
    DiscoveryResultEntity discoveryResultEntity = new DiscoveryResultEntity();
    try {
        session = _dao._database.getNewSession();
        tx = session.beginTransaction();
        String hql = "from DiscoveryResultEntity where refId = :refId";
        Query query = session.createQuery(hql);
        query.setString("refId", newResult.getRefId());
        discoveryResultEntity = (DiscoveryResultEntity) query.setMaxResults(1).uniqueResult();

        if (discoveryResultEntity == null) {
            discoveryResultEntity = new DiscoveryResultEntity();
            discoveryResultEntity.setRefId(newResult.getRefId());
            discoveryResultEntity.setRefType(newResult.getRefType());
            discoveryResultEntity.setParentJobId(newResult.getParentJobId());
            discoveryResultEntity.setJobId(newResult.getJobId());
        }
        discoveryResultEntity.setDeviceRefId(newResult.getDeviceRefId());
        discoveryResultEntity.setDeviceType(newResult.getDeviceType());
        discoveryResultEntity.setStatus(newResult.getStatus());
        discoveryResultEntity.setStatusMessage(newResult.getStatusMessage());
        discoveryResultEntity.setServiceTag(newResult.getServiceTag());
        discoveryResultEntity.setIpaddress(newResult.getIpaddress());
        discoveryResultEntity.setModel(newResult.getModel());
        //logger.info("serer count and iom count: " + newResult.getServerCount() + " : "+newResult.getIomCount());
        discoveryResultEntity.setServerCount(newResult.getServerCount());
        discoveryResultEntity.setIomCount(newResult.getIomCount());
        discoveryResultEntity.setServerType(newResult.getServerType());
        discoveryResultEntity.setHealthState(newResult.getHealthState());
        discoveryResultEntity.setHealthStatusMsg(newResult.getHealthStatusMsg());
        discoveryResultEntity.setVendor(newResult.getVendor());
        discoveryResultEntity.setSystem_id(newResult.getSystem_id());

        discoveryResultEntity.setUpdatedDate(new Date());
        discoveryResultEntity.setUpdatedBy(_dao.extractUserFromRequest());
        discoveryResultEntity.setFacts(newResult.getFacts());
        discoveryResultEntity.setUnmanaged(newResult.isUnmanaged());
        discoveryResultEntity.setReserved(newResult.isReserved());
        discoveryResultEntity.setConfig(newResult.getConfig());
        discoveryResultEntity.setDiscoverDeviceType(newResult.getDiscoverDeviceType());
        discoveryResultEntity.setServerPoolId(newResult.getServerPoolId());

        if (newResult.getFirmwareList() != null)
            for (FirmwareDeviceInventoryEntity fdie : newResult.getFirmwareList()) {
                discoveryResultEntity.addFirmwareDeviceInventoryEntity(fdie);
            }

        session.saveOrUpdate(discoveryResultEntity);

        // Commit transaction.
        tx.commit();
    } catch (ConstraintViolationException cve) {
        logger.warn("Caught exception during discovery result creation: " + cve);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during create discovery result: " + ex);
        }
        if (cve.getConstraintName().contains("refid")) {
            //throw new AsmManagerDAOException(AsmManagerDAOException.REASON_CODE.DUPLICATE_JOBID, cve);
            throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.DUPLICATE_JOBID,
                    AsmManagerMessages.duplicateRefId(cve.getSQLException().getMessage()));
        } else {
            //throw new AsmManagerDAOException(AsmManagerDAOException.REASON_CODE.DUPLICATE_RECORD, cve);
            throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.DUPLICATE_RECORD,
                    AsmManagerMessages.duplicateRefId(cve.getSQLException().getMessage()));
        }
    } catch (Exception e) {
        logger.warn("Caught exception during update device: " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during update device: " + ex);
        }
        if (e instanceof AsmManagerCheckedException) {
            throw e;
        }
        throw new AsmManagerInternalErrorException("Error updating the discovery result", "DiscoveryResultDAO",
                e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during update device: " + ex);
        }
    }
}

From source file:com.dell.asm.asmcore.asmmanager.db.FirmwareRepositoryDAO.java

License:Open Source License

public FirmwareRepositoryEntity getCompleteFirmware(String id, boolean bundles, boolean components) {
    Session session = null;//from  w  w w .  ja  v  a2 s.c o  m
    Transaction tx = null;
    FirmwareRepositoryEntity firmware = null;

    try {
        session = dao.getNewSession();
        tx = session.beginTransaction();

        // Create and execute command.
        String hql = "from FirmwareRepositoryEntity where id = :id";
        Query query = session.createQuery(hql);
        query.setString("id", id);
        firmware = (FirmwareRepositoryEntity) query.setMaxResults(1).uniqueResult();
        if (firmware != null) {
            hydrateFirmware(firmware, bundles, components);
        }
        // Commit transaction.
        tx.commit();
    } catch (Exception e) {
        logger.warn("Caught exception during get firmwarerepository for id: " + id + ", " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during get by id: " + ex);
        }
        throw new AsmManagerInternalErrorException("Retrieve firmwarerepository", "GenericDAO", e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during getCompleteFirmware: " + ex);
        }
    }

    return firmware;

}

From source file:com.dell.asm.asmcore.asmmanager.db.FirmwareRepositoryDAO.java

License:Open Source License

public FirmwareRepositoryEntity getFirmwareWithUsedBy(String id) {
    Session session = null;/*ww w . j av a2  s . c o  m*/
    Transaction tx = null;
    FirmwareRepositoryEntity firmware = null;

    try {
        session = dao.getNewSession();
        tx = session.beginTransaction();

        // Create and execute command.
        String hql = "from FirmwareRepositoryEntity where id = :id";
        Query query = session.createQuery(hql);
        query.setString("id", id);
        firmware = (FirmwareRepositoryEntity) query.setMaxResults(1).uniqueResult();
        if (firmware != null) {
            // initialize templates but not deployments as they are eager fetch.
            Hibernate.initialize(firmware.getTemplates());
        }
        // Commit transaction.
        tx.commit();
    } catch (Exception e) {
        logger.warn("Caught exception during getFirmwareWithUsedBy for id: " + id + ", " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during get by id: " + ex);
        }
        throw new AsmManagerInternalErrorException("Retrieve getFirmwareWithUsedBy", "GenericDAO", e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during getFirmwareWithUsedBy: " + ex);
        }
    }

    return firmware;

}

From source file:com.dell.asm.asmcore.asmmanager.db.MapRazorNodeNameToSerialNumberDAO.java

License:Open Source License

/**
 * Retrieve Device Inventory.//from  w  ww  .  ja v  a2 s  .  c  o  m
 * @return the entity
 */
public MapRazorNodeNameToSerialNumberEntity getRazorNodeById(String Id) {

    Session session = null;
    Transaction tx = null;
    MapRazorNodeNameToSerialNumberEntity deviceInventoryEntity = null;

    try {
        session = _dao._database.getNewSession();
        tx = session.beginTransaction();

        // Create and execute command.
        String hql = "from MapRazorNodeNameToSerialNumberEntity where id = :id";
        Query query = session.createQuery(hql);
        query.setString("id", Id);
        deviceInventoryEntity = (MapRazorNodeNameToSerialNumberEntity) query.setMaxResults(1).uniqueResult();

        // Commit transaction.
        tx.commit();
    } catch (Exception e) {
        logger.warn(
                "Caught exception during get MapRazorNodeNameToSerialNumberEntity for refId: " + Id + ", " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during get device: " + ex);
        }
        throw new AsmManagerInternalErrorException("Retrieve device", "MapRazorNodeNameToSerialNumberDAO", e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during get device: " + ex);
        }
    }

    return deviceInventoryEntity;
}

From source file:com.dell.asm.asmcore.asmmanager.db.MapRazorNodeNameToSerialNumberDAO.java

License:Open Source License

/**
 * Retrieve Device Inventory./*from w  ww . ja v a  2  s.com*/
 * @return the entity
 */
public MapRazorNodeNameToSerialNumberEntity getRazorNodeBySerialNumber(String serialNumber) {

    Session session = null;
    Transaction tx = null;
    MapRazorNodeNameToSerialNumberEntity deviceInventoryEntity = null;

    try {
        session = _dao._database.getNewSession();
        tx = session.beginTransaction();

        // Create and execute command.
        String hql = "from MapRazorNodeNameToSerialNumberEntity where serialNumber = :serialNumber";
        Query query = session.createQuery(hql);
        query.setString("serialNumber", serialNumber);
        deviceInventoryEntity = (MapRazorNodeNameToSerialNumberEntity) query.setMaxResults(1).uniqueResult();

        // Commit transaction.
        tx.commit();
    } catch (Exception e) {
        logger.warn("Caught exception during get MapRazorNodeNameToSerialNumberEntity for serialNumber: "
                + serialNumber + ", " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during get device: " + ex);
        }
        throw new AsmManagerInternalErrorException("Retrieve device", "MapRazorNodeNameToSerialNumberDAO", e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during get device: " + ex);
        }
    }

    return deviceInventoryEntity;
}

From source file:com.dell.asm.asmcore.asmmanager.db.ServiceTemplateDAO.java

License:Open Source License

public ServiceTemplateEntity getTemplateByName(String name) {

    Session session = null;/*  w  w  w  .  j a v  a 2 s  . c o m*/
    Transaction tx = null;
    ServiceTemplateEntity templateEntity = null;

    try {
        session = _dao._database.getNewSession();
        tx = session.beginTransaction();

        // Create and execute command.
        String hql = "from ServiceTemplateEntity where name =:name";
        Query query = session.createQuery(hql);
        query.setString("name", name.trim());
        templateEntity = (ServiceTemplateEntity) query.setMaxResults(1).uniqueResult();

        //            Collection<PolicyRefEntity> dummy;
        //            dummy = templateEntity.getPolicyRefEntities();
        //            dummy.isEmpty();

        // Commit transaction.
        tx.commit();

    } catch (Exception e) {
        logger.warn("Caught exception during get template for template name: " + name + ", " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during get template: " + ex);
        }
        throw new AsmManagerInternalErrorException("Create Template", "TemplateDAO", e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during get device: " + ex);
        }
    }

    return templateEntity;
}

From source file:com.dell.asm.asmcore.asmmanager.db.ServiceTemplateDAO.java

License:Open Source License

public ServiceTemplateEntity getTemplateById(String Id) {

    Session session = null;/*  w ww  . jav a2  s  .  c om*/
    Transaction tx = null;
    ServiceTemplateEntity templateEntity = null;

    try {
        session = _dao._database.getNewSession();
        tx = session.beginTransaction();

        // Create and execute command.
        String hql = "from ServiceTemplateEntity where template_id =:id";
        Query query = session.createQuery(hql);
        query.setString("id", Id);
        templateEntity = (ServiceTemplateEntity) query.setMaxResults(1).uniqueResult();

        // Commit transaction.
        tx.commit();

    } catch (Exception e) {
        logger.warn("Caught exception during get template for template id: " + Id + ", " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during get template: " + ex);
        }
        throw new AsmManagerInternalErrorException("Retrieve Template by Id", "TemplateDAO", e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during get template: " + ex);
        }
    }

    return templateEntity;
}

From source file:com.dell.asm.asmcore.asmmanager.db.ServiceTemplateDAO.java

License:Open Source License

public ServiceTemplateEntity updateTemplate(ServiceTemplateEntity updatedTemplate)
        throws AsmManagerInternalErrorException {
    Session session = null;//from w ww . jav a 2s .c om
    Transaction tx = null;
    ServiceTemplateEntity templateEntity = null;

    try {
        session = _dao._database.getNewSession();
        tx = session.beginTransaction();
        String hql = "from ServiceTemplateEntity where template_id = :id";
        Query query = session.createQuery(hql);
        query.setString("id", updatedTemplate.getTemplateId());
        templateEntity = (ServiceTemplateEntity) query.setMaxResults(1).uniqueResult();

        if (templateEntity != null) {
            templateEntity.setTemplateDesc(updatedTemplate.getTemplateDesc());
            templateEntity.setTemplateVersion(updatedTemplate.getTemplateVersion());
            templateEntity.setTemplateValid(updatedTemplate.isTemplateValid());
            templateEntity.setTemplateLocked(updatedTemplate.isTemplateLocked());
            // templateEntity.setDisplayName(updatedTemplate.getDisplayName());
            // templateEntity.setDeviceType(updatedTemplate.getDeviceType());
            templateEntity.setDraft(updatedTemplate.isDraft());
            templateEntity.setName(updatedTemplate.getName());
            templateEntity.setUpdatedBy(_dao.extractUserFromRequest());

            //TODO remove the set updated date when @PreUpdate is working
            GregorianCalendar now = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
            templateEntity.setUpdatedDate(now);
            templateEntity.setLastDeployedDate(updatedTemplate.getLastDeployedDate());

            templateEntity.setMarshalledTemplateData(updatedTemplate.getMarshalledTemplateData());

            templateEntity.setManageFirmware(updatedTemplate.isManageFirmware());
            templateEntity.setUseDefaultCatalog(updatedTemplate.isUseDefaultCatalog());
            templateEntity.setFirmwareRepositoryEntity(updatedTemplate.getFirmwareRepositoryEntity());

            templateEntity.setAllUsersAllowed(updatedTemplate.isAllUsersAllowed());
            if (templateEntity.getAssignedUserList() != null) {
                templateEntity.getAssignedUserList().clear();
            } else {
                Set<TemplateUserRefEntity> userRefEntities = new HashSet<>();
                templateEntity.setAssignedUserList(userRefEntities);
            }

            for (TemplateUserRefEntity uRef : updatedTemplate.getAssignedUserList()) {
                uRef.setId(UUID.randomUUID().toString());
                templateEntity.getAssignedUserList().add(uRef);
            }

            updateAddOnModules(templateEntity, updatedTemplate);

            session.saveOrUpdate(templateEntity);

            //commit
            tx.commit();
        } else {
            String msg = "unable to update template with name " + updatedTemplate.getName();
            logger.warn(msg);
        }

    } catch (Exception e) {
        logger.warn("Caught exception during update template: " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during update template: " + ex);
        }
        // TODO: Reviewer: instanceof will always return false since a RuntimeException can't be a com.dell.asm.asmcore.asmmanager.exception.AsmManagerCheckedException
        //if (e instanceof AsmManagerCheckedException) {
        //    throw e;
        //}
        throw new AsmManagerInternalErrorException("update Template", "TemplateDAO", e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during update template: " + ex);
        }
    }

    return templateEntity;

}

From source file:com.dell.asm.asmcore.asmmanager.db.ServiceTemplateDAO.java

License:Open Source License

public ServiceTemplateEntity deleteTemplate(String id) {

    logger.info("Deleting template Id : " + id);
    Session session = null;/*from  w w  w  .j ava 2  s.c  o m*/
    Transaction tx = null;

    try {
        session = _dao._database.getNewSession();
        tx = session.beginTransaction();
        String hql = "from ServiceTemplateEntity where template_id =:id";
        Query query = session.createQuery(hql);
        query.setString("id", id);
        ServiceTemplateEntity template = (ServiceTemplateEntity) query.setMaxResults(1).uniqueResult();

        if (template != null) {
            session.delete(template);
        }
        // Commit transaction.
        tx.commit();
        return template;
    } catch (Exception e) {
        logger.warn("Caught exception during delete template: " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during delete template: " + ex);
        }
        throw new AsmManagerInternalErrorException("Delete Template", "TemplateDAO", e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during delete template: " + ex);
        }
    }
}

From source file:com.dell.asm.asmcore.asmmanager.db.TemplateDAO.java

License:Open Source License

public TemplateEntity getTemplateByName(String name) {

    Session session = null;//  w  w  w .  ja v a 2 s.com
    Transaction tx = null;
    TemplateEntity templateEntity = null;

    try {
        session = _dao._database.getNewSession();
        tx = session.beginTransaction();

        // Create and execute command.
        String hql = "from TemplateEntity where name =:name";
        Query query = session.createQuery(hql);
        query.setString("name", name);
        templateEntity = (TemplateEntity) query.setMaxResults(1).uniqueResult();

        //            Collection<PolicyRefEntity> dummy;
        //            dummy = templateEntity.getPolicyRefEntities();
        //            dummy.isEmpty();

        // Commit transaction.
        tx.commit();

    } catch (Exception e) {
        logger.warn("Caught exception during get template for template name: " + name + ", " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during get template: " + ex);
        }
        throw new AsmManagerInternalErrorException("Create Template", "TemplateDAO", e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during get device: " + ex);
        }
    }

    return templateEntity;
}