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

License:Open Source License

/**
 * Retrieve DeviceDiscoverEntity based on id.
 * /*from  w w  w.  j a  v  a2 s . co  m*/
 * @return the DeviceDiscoverEntity.
 */
public DeviceDiscoverEntity getDeviceDiscoverEntityById(String id) throws AsmManagerDAOException {

    Session session = null;
    Transaction tx = null;
    DeviceDiscoverEntity deviceDiscoverEntity = null;

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

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

        // Commit transaction.
        tx.commit();

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

    return deviceDiscoverEntity;
}

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

License:Open Source License

public boolean updateDeviceDiscoverEntity(DeviceDiscoverEntity deviceDiscoverEntity)
        throws AsmManagerCheckedException {

    Session session = null;/*  w  w w . ja v  a 2s .c  om*/
    Transaction tx = null;
    boolean updatedFlag = false;

    try {
        session = _dao._database.getNewSession();
        tx = session.beginTransaction();
        String hql = "from DeviceDiscoverEntity where id = :id";
        Query query = session.createQuery(hql);
        query.setString("id", deviceDiscoverEntity.getId());

        DeviceDiscoverEntity databaseDeviceDiscoverEntity = (DeviceDiscoverEntity) query.setMaxResults(1)
                .uniqueResult();
        if (databaseDeviceDiscoverEntity == null) {
            throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.RECORD_NOT_FOUND,
                    AsmManagerMessages.notFound(deviceDiscoverEntity.getId()));
        }

        databaseDeviceDiscoverEntity.setStatus(deviceDiscoverEntity.getStatus());
        databaseDeviceDiscoverEntity.setUpdatedDate(new GregorianCalendar());
        databaseDeviceDiscoverEntity.setUpdatedBy(_dao.extractUserFromRequest());

        session.saveOrUpdate(databaseDeviceDiscoverEntity);

        // Commit transaction.
        tx.commit();
        updatedFlag = true;
    } catch (Exception e) {
        logger.warn("Caught exception during update device discover entity : " + e);
        try {
            if (tx != null) {
                tx.rollback();
                updatedFlag = false;
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during update device discover entity : " + ex);
        }
        if (e instanceof AsmManagerCheckedException) {
            throw e;
        }
        throw new AsmManagerInternalErrorException("Update device discover entity", "DeviceDiscoverDAO", e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during update device discover entity : " + ex);
        }
    }

    return updatedFlag;
}

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

License:Open Source License

/**
 * Update Device Group//from www. ja  v a  2s .  c om
 * 
 * @return updateEntity - updated device group entity
 * 
 * @throws AsmManagerCheckedException
 */
public DeviceGroupEntity updateGroupDevice(DeviceGroupEntity updateEntity) throws AsmManagerCheckedException {

    if (updateEntity == null) {
        return null;
    }
    // Initialize locals.
    Session session = null;
    Transaction tx = null;
    DeviceGroupEntity entity = null;

    // Save the job history in the db.
    try {
        session = _dao._database.getNewSession();
        tx = session.beginTransaction();

        // Save the new DeviceGroupEntity.
        String hql = "from DeviceGroupEntity where seqId = :id";
        Query query = session.createQuery(hql);
        query.setLong("id", updateEntity.getSeqId());
        entity = (DeviceGroupEntity) query.setMaxResults(1).uniqueResult();

        if (null != entity) {

            if (null != updateEntity.getName() || !"".equals(updateEntity.getName()))
                entity.setName(updateEntity.getName());

            if (null != updateEntity.getDescription())
                entity.setDescription(updateEntity.getDescription());

            entity.setUpdatedBy(_dao.extractUserFromRequest());
            entity.setUpdatedDate(new GregorianCalendar());

            if (null != updateEntity.getDeviceInventories())
                entity.setDeviceInventories(updateEntity.getDeviceInventories());

            if (null != updateEntity.getGroupsUsers())
                entity.setGroupsUsers(updateEntity.getGroupsUsers());

            session.saveOrUpdate(entity);

            // Commit transaction and clean up.
            tx.commit();
        } else {
            throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.INVALID_REQUEST,
                    AsmManagerMessages.updateDeviceGroupError(String.valueOf(updateEntity.getSeqId())));
        }

    } catch (Exception e) {
        logger.warn("Caught exception during updating device group: " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during updating device group: " + ex);
        }
        throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.INVALID_REQUEST,
                AsmManagerMessages.updateDeviceGroupError(String.valueOf(updateEntity.getSeqId())));
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during updating device group: " + ex);
        }
    }
    return entity;
}

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

License:Open Source License

/**
 * Retrieve Device Inventory.//from   w ww.ja  va 2 s.  co  m
 *
 * @return the entity
 */
public DeviceInventoryEntity getDeviceInventory(String refId) {

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

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

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

        if (deviceInventoryEntity != null) {
            Hibernate.initialize(deviceInventoryEntity.getDeviceInventoryComplianceEntities());
        }

        this.setFirmwareBasedOnDeployment(deviceInventoryEntity);
        // Commit transaction.
        tx.commit();
    } catch (Exception e) {
        logger.warn("Caught exception during get device for refId: " + refId + ", " + e, e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.error("Unable to rollback transaction during get device: " + refId, ex);
        }
        // not found landed hee 
        // throw new AsmManagerInternalErrorException("Retrieve device", "DeviceInventoryDAO", 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.DeviceInventoryDAO.java

License:Open Source License

public DeviceInventoryEntity getDeviceInventoryByServiceTag(String serviceTag) {

    Session session = null;/*from  w w w .ja va 2  s.  co m*/
    Transaction tx = null;
    DeviceInventoryEntity deviceInventoryEntity = null;

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

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

        this.setFirmwareBasedOnDeployment(deviceInventoryEntity);

        // Commit transaction.
        tx.commit();
    } catch (Exception e) {
        logger.warn("Caught exception during get device for serviceTag: " + serviceTag + ", " + e, 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", "DeviceInventoryDAO", 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.DeviceInventoryDAO.java

License:Open Source License

/**
 * Update Device Inventory.//from   w w w  . ja  va2 s . c o  m
 *
 * @param newDevice the device to update.
 */
public void updateDeviceInventory(DeviceInventoryEntity newDevice) throws AsmManagerCheckedException {

    Session session = null;
    Transaction tx = null;
    logger.info("DeviceInventoryDAO.updateDeviceInventory for device with refId " + newDevice.getRefId());
    logger.info(
            "DeviceInventoryDAO.updateDeviceInventory for device with ipAddress " + newDevice.getIpAddress());
    logger.info(
            "DeviceInventoryDAO.updateDeviceInventory for device with serviceTag " + newDevice.getServiceTag());

    try {
        session = _dao._database.getNewSession();
        tx = session.beginTransaction();
        String hql = "from DeviceInventoryEntity where refId = :refId";
        Query query = session.createQuery(hql);
        query.setString("refId", newDevice.getRefId());
        DeviceInventoryEntity deviceInventoryEntity = (DeviceInventoryEntity) query.setMaxResults(1)
                .uniqueResult();
        if (deviceInventoryEntity == null) {
            throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.RECORD_NOT_FOUND,
                    AsmManagerMessages.notFound(newDevice.getRefId()));
        }

        deviceInventoryEntity.setRefType(newDevice.getRefType());
        deviceInventoryEntity.setDeviceType(newDevice.getDeviceType());
        deviceInventoryEntity.setManagedState(newDevice.getManagedState());
        deviceInventoryEntity.setState(newDevice.getState());
        deviceInventoryEntity.setDisplayName(newDevice.getDisplayName());
        deviceInventoryEntity.setServiceTag(newDevice.getServiceTag());
        deviceInventoryEntity.setIpAddress(newDevice.getIpAddress());
        deviceInventoryEntity.setModel(newDevice.getModel());
        deviceInventoryEntity.setComplianceCheckDate(newDevice.getComplianceCheckDate());
        deviceInventoryEntity.setDiscoveredDate(newDevice.getDiscoveredDate());
        deviceInventoryEntity.setHealth(newDevice.getHealth());
        deviceInventoryEntity.setHealthMessage(newDevice.getHealthMessage());
        deviceInventoryEntity.setInfraTemplateDate(newDevice.getInfraTemplateDate());
        deviceInventoryEntity.setInfraTemplateId(newDevice.getInfraTemplateId());
        deviceInventoryEntity.setIdentityRefId(newDevice.getIdentityRefId());
        deviceInventoryEntity.setInventoryDate(newDevice.getInventoryDate());
        deviceInventoryEntity.setServerTemplateDate(newDevice.getServerTemplateDate());
        deviceInventoryEntity.setServerTemplateId(newDevice.getServerTemplateId());
        deviceInventoryEntity.setConfig(newDevice.getConfig());
        deviceInventoryEntity.setSystemId(newDevice.getSystemId());
        deviceInventoryEntity.setOsImageType(newDevice.getOsImageType());
        deviceInventoryEntity.setOsAdminPassword(newDevice.getOsAdminPassword());
        deviceInventoryEntity.setOsIpAddress(newDevice.getOsIpAddress());
        deviceInventoryEntity.setFailuresCount(newDevice.getFailuresCount());
        deviceInventoryEntity.setCompliant(newDevice.getCompliant());
        deviceInventoryEntity.setChassisId(newDevice.getChassisId());
        deviceInventoryEntity.setUpdatedDate(new GregorianCalendar());
        deviceInventoryEntity.setUpdatedBy(_dao.extractUserFromRequest());
        if (newDevice.getDeviceGroupList() != null && !newDevice.getDeviceGroupList().isEmpty()) {
            deviceInventoryEntity.setDeviceGroupList(newDevice.getDeviceGroupList());
        }

        deviceInventoryEntity.setFacts(newDevice.getFacts());
        session.saveOrUpdate(deviceInventoryEntity);

        // Commit transaction.
        tx.commit();
    } catch (Exception e) {
        logger.warn("Caught exception during update device: " + e, e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during update device: " + ex);
        }
        if (e instanceof StaleObjectStateException) {
            StaleObjectStateException sose = ((StaleObjectStateException) e);
            logger.warn("StaleObjectStateException for device with refId " + newDevice.getRefId());
            logger.warn("StaleObjectStateException for device with ipAddress " + newDevice.getIpAddress());
            logger.warn("StaleObjectStateException for device with serviceTag " + newDevice.getServiceTag());
        }

        if (e instanceof AsmManagerCheckedException) {
            throw e;
        }
        throw new AsmManagerInternalErrorException("Update device", "DeviceInventoryDAO", 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.DeviceInventoryDAO.java

License:Open Source License

/**
 * Helper method to get the device ref Id based on the Service Tag
 *
 * @param serviceTag - a string that is hte service tag of hte device
 * @return String - the Ref Id of the device
 *//*w  ww  .j  a  v  a2  s.  co  m*/
public String getRefIdOfDevice(String serviceTag) {
    logger.info("Retrieving ref Id of Device from inventory with service tag : " + serviceTag);
    Session session = null;
    Transaction tx = null;

    String refIdString = "";

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

        String hql = "select refId from DeviceInventoryEntity where serviceTag = :serviceTag";
        Query query = session.createQuery(hql);
        query.setString("serviceTag", serviceTag);
        refIdString = (String) query.setMaxResults(1).uniqueResult();

        // Commit transaction.
        tx.commit();
    } catch (Exception e) {
        logger.warn("Caught exception during get ref ID from device inventory from service tag: " + e, e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction: " + ex);
        }
        throw new AsmManagerInternalErrorException("Get Ref Id from Service Tag", "DeviceInventoryDAO", e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during ref Id retrieval: " + ex);
        }
    }

    return refIdString;
}

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

License:Open Source License

/**
 * Helper method to get the device ref Id based on the Service Tag
 *
 * @param ipAddress - a string that is the ipAddress of the device
 * @return String - the Ref Id of the device
 *//*from w  w  w  .  ja  v  a2s .  com*/
public String getRefIdOfDeviceByIpAddress(String ipAddress) {
    logger.info("Retrieving ref Id of Device from inventory with ipAddress : " + ipAddress);
    Session session = null;
    Transaction tx = null;

    String refIdString = "";

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

        String hql = "select refId from DeviceInventoryEntity where ipAddress = :ipAddress";
        Query query = session.createQuery(hql);
        query.setParameter("ipAddress", ipAddress);
        refIdString = (String) query.setMaxResults(1).uniqueResult();

        // Commit transaction.
        tx.commit();
    } catch (Exception e) {
        logger.warn("Caught exception during get ref ID from device inventory by ipAddress: " + e, e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction: " + ex);
        }
        throw new AsmManagerInternalErrorException("Get Ref Id by ipAddress", "DeviceInventoryDAO", e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during ref Id retrieval: " + ex);
        }
    }

    return refIdString;
}

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

License:Open Source License

public DeviceLastJobStateEntity getLastJob(String deviceRefId, JobType jobType)
        throws AsmManagerCheckedException {
    Session session = null;/*from w w w  .j a va  2s . c  om*/
    Transaction tx = null;
    DeviceLastJobStateEntity lastJob = null;

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

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

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

    return lastJob;

}

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

License:Open Source License

/**
 * Create a new or update existing last job state table based on given device ref id
 * and job type.//from w  w w  .  j av  a2 s .  c om
 *
 * @param deviceRefId
 * @param jobType
 * @param successful
 * @param description
 */
public void createOrUpdateLastJob(String deviceRefId, JobType jobType, DeviceState successful,
        String description) {
    Session session = null;
    Transaction tx = null;

    try {
        session = _dao._database.getNewSession();
        tx = session.beginTransaction();
        String hql = "from DeviceLastJobStateEntity where deviceRefId = :deviceRefId and jobType = :jobType";
        Query query = session.createQuery(hql);
        query.setString("deviceRefId", deviceRefId);
        query.setString("jobType", jobType.name());
        DeviceLastJobStateEntity lastJob = (DeviceLastJobStateEntity) query.setMaxResults(1).uniqueResult();
        if (lastJob == null) {
            lastJob = new DeviceLastJobStateEntity();
            lastJob.setDeviceRefId(deviceRefId);
            lastJob.setJobType(jobType);
            lastJob.setCreatedDate(new GregorianCalendar());
        } else {
            lastJob.setUpdatedDate(new GregorianCalendar());
        }
        lastJob.setDescription(description);
        lastJob.setJobState(successful);

        session.saveOrUpdate(lastJob);

        // Commit transaction.
        tx.commit();
    } catch (Exception e) {
        logger.warn("Caught exception during create or update last job: ", e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during create or update last job: " + ex);
        }
        throw new AsmManagerInternalErrorException("Create or Update Last Job", "DeviceInventoryDAO", e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during create or update last job: " + ex);
        }
    }
}