Example usage for org.hibernate Query setString

List of usage examples for org.hibernate Query setString

Introduction

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

Prototype

@Deprecated
@SuppressWarnings("unchecked")
default Query<R> setString(String name, String val) 

Source Link

Document

Bind a named String-valued parameter.

Usage

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

License:Open Source License

/**
 * Retrieve Device Inventory.//from ww w .  j  a  v a 2s. c  o m
 * @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;/*  ww  w . ja v a 2 s  . co 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;/*from   w w w . ja v a 2s  .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;//ww  w  .j  av a  2s  . c o m
    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  ww w .  ja  v a  2 s.  com*/
    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;//from w  w  w  .j ava 2  s . co  m
    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;
}

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

License:Open Source License

public TemplateEntity getTemplateById(String Id) {

    Session session = null;/*from w ww . ja v  a2  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 template_id =:id";
        Query query = session.createQuery(hql);
        query.setString("id", Id);
        templateEntity = (TemplateEntity) 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.TemplateDAO.java

License:Open Source License

public TemplateEntity updateTemplate(TemplateEntity updatedTemplate) throws AsmManagerInternalErrorException {
    Session session = null;//from  www  .j  a va  2  s .  c  o  m
    Transaction tx = null;
    TemplateEntity templateEntity = null;

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

        if (templateEntity != null) {
            templateEntity.setTemplateType(updatedTemplate.getTemplateType());
            templateEntity.setTemplateDesc(updatedTemplate.getTemplateDesc());
            // templateEntity.setDisplayName(updatedTemplate.getDisplayName());
            // templateEntity.setDeviceType(updatedTemplate.getDeviceType());
            templateEntity.setState(updatedTemplate.getState());
            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.setMarshalledTemplateData(updatedTemplate.getMarshalledTemplateData());
            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.TemplateDAO.java

License:Open Source License

public void deleteTemplate(String id) {

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

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

        if (template != null) {
            session.delete(template);
        }
        // Commit transaction.
        tx.commit();
    } 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.dotosoft.dotoquiz.tools.util.HibernateUtil.java

License:Apache License

public static ParameterQuestionType getQuestionTypeByName(Object sessionTmp, Object nameTmp) {
    Session session = (Session) sessionTmp;
    String name = (String) nameTmp;
    Query q = session.createQuery("From ParameterQuestionType qt where qt.name = :questionTypeId");
    q.setString("questionTypeId", name);

    ParameterQuestionType parameterQuestionType = (ParameterQuestionType) q.uniqueResult();
    return parameterQuestionType;
}