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.copyright.common.hibernate.SimpleHibernateDao.java

License:Apache License

/**
 * ?HQL?Query./*from   ww w .  j a  v  a 2s  .  c  om*/
 * 
 * ?find()T,?T.
 * 
 * @param values ????,?.
 */
public Query createQuery(final String queryString, final Object... values) {
    Assert.hasText(queryString, "queryString?");
    Query query = getSession().createQuery(queryString);
    if (values != null && values.length > 0) {
        if (values[0] instanceof Hashtable) {
            Hashtable temp = (Hashtable) values[0];
            query = setParamHash(query, temp);
        } else {
            if (values[0] instanceof List) {
                List temp = (List) values[0];
                int size = temp.size();
                for (int i = 0; i < size; i++) {
                    Object param = temp.get(i);
                    //query.setParameter(i,values[i]); 
                    if (param instanceof String) {
                        String paramValue = (String) param;
                        query.setString(i, paramValue);
                    } else {
                        if (param instanceof Integer) {
                            Integer paramValue = (Integer) param;
                            query.setInteger(i, paramValue.intValue());
                        } else {
                            if (param instanceof Long) {
                                Long paramValue = (Long) param;
                                query.setLong(i, paramValue.longValue());
                            } else {
                                if (param instanceof Double) {
                                    Double paramValue = (Double) param;
                                    query.setDouble(i, paramValue.doubleValue());
                                } else {
                                    if (param instanceof Float) {
                                        Float paramValue = (Float) param;
                                        query.setFloat(i, paramValue.floatValue());
                                    }
                                }
                            }
                        }
                    }

                }
            } else {
                for (int i = 0; i < values.length; i++) {
                    query.setParameter(i, values[i]);
                }
            }

        }
    }

    return query;
}

From source file:com.copyright.common.hibernate.SimpleHibernateDao.java

License:Apache License

/**
 * //  www  . j  a va2  s  .co  m
 * @param query
 * @param values
 * @return
 */
private Query setParamHash(Query query, Hashtable<String, ?> values) {
    if (values != null) {
        //query.setProperties(values);
        Enumeration parameterNames = values.keys();
        while (parameterNames.hasMoreElements() == true) {
            String pName = (String) parameterNames.nextElement();
            Object param = values.get(pName);
            if (param instanceof String) {
                String paramValue = (String) param;
                query.setString(pName, paramValue);
            } else {
                if (param instanceof Integer) {
                    Integer paramValue = (Integer) param;
                    query.setInteger(pName, paramValue.intValue());
                } else {
                    if (param instanceof Double) {
                        Double paramValue = (Double) param;
                        query.setDouble(pName, paramValue.doubleValue());
                    }
                }

            }

        }

    }
    return query;
}

From source file:com.dao.TelefonosDao.java

public Telefonos getTelefono(String telefono) throws HibernateException {
    Telefonos tel = null;/*from  www.j av  a 2 s. c om*/
    try {
        iniciaOperacion();
        String queryString = "from Telefonos where telefono_area = :telefono";
        Query query = sesion.createQuery(queryString);
        query.setString("telefono", telefono);
        tel = (Telefonos) query.uniqueResult();
        System.out.println("obteniendo los telefonos de un usuario" + tel.getTelefonoArea());
    } catch (Exception e) {
        System.out.println("No se ha encontrado el registro telefonico");
        e.printStackTrace();

    } finally {
        sesion.close();

    }

    return tel;
}

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

License:Open Source License

/**
 * Retrieve DeviceConfigureEntity based on id.
 * //from   w ww . ja  va2 s .com
 * @return the DeviceConfigureEntity.
 */
public DeviceConfigureEntity getDeviceConfigureEntityById(String id) throws AsmManagerDAOException {

    Session session = null;
    Transaction tx = null;
    DeviceConfigureEntity deviceConfigureEntity = null;

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

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

        // Commit transaction.
        tx.commit();

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

    return deviceConfigureEntity;
}

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

License:Open Source License

/**
 * Delete DeviceConfigureEntity by id./*from  w  w  w.  j  av a  2  s.  c  o  m*/
 * 
 * @param id
 */
public void deleteDiscoveryResult(String id) {

    Session session = null;
    Transaction tx = null;

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

        // execute command.
        String hql = "delete DeviceConfigureEntity  where id=:id";
        Query query = session.createQuery(hql);
        query.setString("id", id);
        int rowCount = query.executeUpdate();
        logger.debug("Deleted record count=" + rowCount);
        // Commit transaction.
        tx.commit();
    } catch (Exception e) {
        logger.warn("Caught exception during delete DeviceConfigureEntity: " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during delete DeviceConfigureEntity: " + ex);
        }

        throw new AsmManagerInternalErrorException("Error deleting DeviceConfigureEntity", "DeviceConfigureDAO",
                e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during delete DeviceConfigureEntity: " + ex);
        }
    }
}

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

License:Open Source License

public boolean updateDeviceConfigureEntity(DeviceConfigureEntity deviceConfigureEntity, ConfigureStatus status)
        throws AsmManagerCheckedException {

    Session session = null;//from w w  w .j av  a2s  .c  o m
    Transaction tx = null;
    boolean updatedFlag = false;

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

        DeviceConfigureEntity databaseDeviceConfigureEntity = (DeviceConfigureEntity) query.setMaxResults(1)
                .uniqueResult();
        if (databaseDeviceConfigureEntity == null) {
            throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.RECORD_NOT_FOUND,
                    AsmManagerMessages.notFound(deviceConfigureEntity.getId()));
        }

        databaseDeviceConfigureEntity.setStatus(status);
        databaseDeviceConfigureEntity.setUpdatedDate(new GregorianCalendar());
        databaseDeviceConfigureEntity.setUpdatedBy(_dao.extractUserFromRequest());

        session.saveOrUpdate(databaseDeviceConfigureEntity);

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

    return updatedFlag;
}

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

License:Open Source License

/**
 * Retrieve DeviceDiscoverEntity based on id.
 * /*from   ww  w .  ja  v  a 2  s.c  om*/
 * @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

/**
 * Delete DeviceDiscoverEntity by id./*from w  w  w . j  a  va 2  s. co m*/
 * 
 * @param id
 */
public void deleteDiscoveryResult(String id) {

    Session session = null;
    Transaction tx = null;

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

        // execute command.
        String hql = "delete DeviceDiscoverEntity  where id=:id";
        Query query = session.createQuery(hql);
        query.setString("id", id);
        int rowCount = query.executeUpdate();
        logger.debug("Deleted record count=" + rowCount);
        // Commit transaction.
        tx.commit();
    } catch (Exception e) {
        logger.warn("Caught exception during delete DeviceDiscoverEntity: " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during delete DeviceDiscoverEntity: " + ex);
        }

        throw new AsmManagerInternalErrorException("Error deleting DeviceDiscoverEntity", "DeviceDiscoverDAO",
                e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during delete DeviceDiscoverEntity: " + ex);
        }
    }
}

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;/*from ww  w.j a  va2s  .c o m*/
    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.DeviceInventoryComplianceDAO.java

License:Open Source License

public void delete(final String deviceInventoryId, final String firmwareRepositoryId) {
    if (StringUtils.isBlank(deviceInventoryId) || StringUtils.isBlank(firmwareRepositoryId)) {
        return;// ww  w . java 2s. c om
    }
    logger.info(
            "Deleting device inventory compliance mapping: " + deviceInventoryId + "," + firmwareRepositoryId);
    Session session = null;
    Transaction tx = null;

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

        String hql = "delete DeviceInventoryComplianceEntity dic "
                + " where dic.deviceInventoryComplianceId.deviceInventoryId = :deviceInventoryId"
                + " and dic.deviceInventoryComplianceId.firmwareRepositoryId = :firmwareRepositoryId";
        Query query = session.createQuery(hql);
        query.setString("deviceInventoryId", deviceInventoryId);
        query.setString("firmwareRepositoryId", firmwareRepositoryId);
        query.executeUpdate();

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