Example usage for org.hibernate Query getQueryString

List of usage examples for org.hibernate Query getQueryString

Introduction

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

Prototype

String getQueryString();

Source Link

Document

Get the query string.

Usage

From source file:kr.debop4j.data.hibernate.repository.impl.HibernateDao.java

License:Apache License

@Override
public <T> PaginatedList<T> getPage(Class<T> clazz, Query query, int pageNo, int pageSize,
        HibernateParameter... parameters) {
    Query countQuery = getSession().createQuery(query.getQueryString());
    long itemCount = count(clazz, countQuery, parameters);

    int firstResult = (pageNo - 1) * pageSize;
    List<T> list = find(clazz, query, firstResult, pageSize, parameters);
    return new PaginatedList(list, pageNo, pageSize, itemCount);
}

From source file:kr.debop4j.data.hibernate.repository.impl.HibernateRepository.java

License:Apache License

@Override
public <E> PaginatedList<E> getPage(Query query, int pageNo, int pageSize, HibernateParameter... parameters) {

    Query countQuery = getSession().createQuery(query.getQueryString());
    long itemCount = count(countQuery, parameters);

    int firstResult = (pageNo - 1) * pageSize;
    List<E> list = find(query, firstResult, pageSize, parameters);
    return new PaginatedList(list, pageNo, pageSize, itemCount);
}

From source file:kr.debop4j.data.hibernate.repository.impl.HibernateRepository.java

License:Apache License

@Override
public <E> E findUniqueByNamedQuery(String queryName, HibernateParameter... parameters) {
    if (isTraceEnabled)
        log.trace("NamedQuery . queryName=[{}], parameters=[{}]", queryName,
                StringTool.listToString(parameters));

    Query query = getSession().getNamedQuery(queryName);

    if (isTraceEnabled)
        log.trace("NamedQuery . queryName=[{}], queryString=[{}], parameters=[{}]", queryName,
                query.getQueryString(), StringTool.listToString(parameters));

    return findUnique(query, parameters);
}

From source file:model.FuncionarioModel.java

public Object validaCPFeCNPJ(String texto) {
    Session objSession = this.objSessionFactory.openSession();
    try {/*from ww w . ja va2s.co  m*/
        Query q = objSession.createSQLQuery("select validaCPFeCNPJ(:texto)");
        q.setParameter("texto", texto);
        System.out.println(q.getQueryString());

        return q.uniqueResult();
    } catch (Exception e) {

    }
    return null;
}

From source file:moos.ssds.dao.DeviceDAO.java

License:LGPL

/**
 * This method returns all <code>Device</code>s that are linked (normally
 * means owned) by a <code>Person</code>.
 * /*w w w. jav a  2 s  .co  m*/
 * @param person
 *            is the <code>Person</code> that will be used to search for
 *            devices.
 * @return a <code>Collection</code> of devices that are linked to that
 *         person.
 */
public Collection findByPerson(Person person, String orderByPropertyName, String ascendingOrDescending,
        boolean returnFullObjectGraph) throws MetadataAccessException {

    // The collection to return
    Collection devices = new ArrayList();

    // First validate the incoming person
    if (person == null) {
        return devices;
    }

    // First make sure the person exists
    PersonDAO personDAO = new PersonDAO(getSession());

    // Try to find the equivalent persistent person
    Long personId = personDAO.findId(person);
    if (personId == null) {
        throw new MetadataAccessException(
                "No person was found in the data store" + " that matched the specified person");
    }

    // Construct query and run it
    StringBuffer queryStringBuffer = new StringBuffer();
    queryStringBuffer.append("from Device device ");
    queryStringBuffer.append("where device.person.id = :personId ");
    // Add order by clause
    if (this.checkIfPropertyOK(orderByPropertyName)) {
        queryStringBuffer
                .append(this.getOrderByPropertyNameSQLClause(orderByPropertyName, ascendingOrDescending));
    }
    try {
        Query query = getSession().createQuery(queryStringBuffer.toString());
        query.setString("personId", personId.toString());
        logger.debug("Compiled query = " + query.getQueryString());
        devices = query.list();
    } catch (HibernateException e) {
        throw new MetadataAccessException(e);
    }

    // Check relationship init
    if (returnFullObjectGraph)
        devices = getRealObjectsAndRelationships(devices);

    // Now return the real objects
    return devices;
}

From source file:moos.ssds.dao.DeviceDAO.java

License:LGPL

/**
 * This method returns all <code>Device</code>s that are of a certain
 * <code>DeviceType</code>/* w w w . j  a va  2  s . co  m*/
 * 
 * @param deviceType
 *            is the <code>DeviceType</code> that will be used to search for
 *            devices.
 * @return a <code>Collection</code> of devices that are categorized by the
 *         given <code>DeviceType</code>
 */
public Collection findByDeviceType(DeviceType deviceType, String orderByPropertyName,
        String ascendingOrDescending, boolean returnFullObjectGraph) throws MetadataAccessException {

    // The collection to return
    Collection devices = new ArrayList();

    // First validate the incoming deviceType
    if (deviceType == null) {
        return devices;
    }

    // Get the DeviceTypeDAO and check the existing of the supplied
    // DeviceType
    DeviceTypeDAO deviceTypeDAO = new DeviceTypeDAO(getSession());

    // Try to find the equivalent persistent deviceType
    Long deviceTypeId = deviceTypeDAO.findId(deviceType);
    if (deviceTypeId == null) {
        throw new MetadataAccessException(
                "No DeviceType was found in the data store" + " that matched the specified DeviceType");
    }

    // Construct the query and run it
    StringBuffer queryStringBuffer = new StringBuffer();
    queryStringBuffer.append("from Device device ");
    queryStringBuffer.append("where device.deviceType.id = :deviceTypeId ");
    // Add order by clause
    if (this.checkIfPropertyOK(orderByPropertyName)) {
        queryStringBuffer
                .append(this.getOrderByPropertyNameSQLClause(orderByPropertyName, ascendingOrDescending));
    }
    try {
        Query query = getSession().createQuery(queryStringBuffer.toString());
        query.setString("deviceTypeId", deviceTypeId.toString());
        logger.debug("Compiled query = " + query.getQueryString());
        devices = query.list();
    } catch (HibernateException e) {
        throw new MetadataAccessException(e);
    }

    if (returnFullObjectGraph)
        devices = getRealObjectsAndRelationships(devices);

    // Now return the real objects
    return devices;
}

From source file:moos.ssds.dao.DeviceDAO.java

License:LGPL

public Collection findBySearchingFields(String id, String uuid, String name, String description,
        String mfgModel, String mfgName, String mfgSerialNumber, String orderByPropertyName,
        String ascendingOrDescending, boolean returnFullObjectGraph) throws MetadataAccessException {

    // The Collection to return
    Collection devices = new ArrayList();

    // First check to see if all fields are empty, if they are, just return
    // the empty collection
    if (isStringEmpty(id) && isStringEmpty(uuid) && isStringEmpty(name) && isStringEmpty(description)
            && isStringEmpty(mfgModel) && isStringEmpty(mfgName) && isStringEmpty(mfgSerialNumber))
        return devices;

    // Since something was specified, formulate the query
    StringBuffer queryBuffer = new StringBuffer();
    queryBuffer.append("from Device device where ");

    // A flag to mark if a clause was specified as the first one
    boolean firstClauseSpecified = false;

    // Check for an ID
    if (!isStringEmpty(id)) {
        queryBuffer.append("device.id = :idSearchTerm");
        firstClauseSpecified = true;/*from  www.  j a  v  a2  s .  co  m*/
    }
    // Add the UUID clause if specified
    if (!isStringEmpty(uuid)) {
        if (firstClauseSpecified)
            queryBuffer.append(" and ");
        queryBuffer.append("device.uuid like :uuidSearchTerm");
        firstClauseSpecified = true;
    }
    // Add the name clause if specified
    if (!isStringEmpty(name)) {
        if (firstClauseSpecified)
            queryBuffer.append(" and ");
        queryBuffer.append("device.name like :nameSearchTerm");
        firstClauseSpecified = true;
    }
    // Add the description clause if specified
    if (!isStringEmpty(description)) {
        if (firstClauseSpecified)
            queryBuffer.append(" and ");
        queryBuffer.append("device.description like :descriptionSearchTerm");
        firstClauseSpecified = true;
    }
    // Add the manufacturer's model if specified
    if (!isStringEmpty(mfgModel)) {
        if (firstClauseSpecified)
            queryBuffer.append(" and ");
        queryBuffer.append("device.mfgModel like :mfgModelSearchTerm");
        firstClauseSpecified = true;

    }
    // Add the manufacturer's name if specified
    if (!isStringEmpty(mfgName)) {
        if (firstClauseSpecified)
            queryBuffer.append(" and ");
        queryBuffer.append("device.mfgName like :mfgNameSearchTerm");
        firstClauseSpecified = true;

    }
    // Add the serial number if specified
    if (!isStringEmpty(mfgSerialNumber)) {
        if (firstClauseSpecified)
            queryBuffer.append(" and ");
        queryBuffer.append("device.mfgSerialNumber like :mfgSerialNumberSearchTerm");
        firstClauseSpecified = true;

    }

    // Check for ordering criteria
    if (this.checkIfPropertyOK(orderByPropertyName)) {
        queryBuffer.append(this.getOrderByPropertyNameSQLClause(orderByPropertyName, ascendingOrDescending));
    }
    try {
        Query query = getSession().createQuery(queryBuffer.toString());
        if (!isStringEmpty(id))
            query.setString("idSearchTerm", id);
        if (!isStringEmpty(uuid))
            query.setString("uuidSearchTerm", "%" + uuid + "%");
        if (!isStringEmpty(name))
            query.setString("nameSearchTerm", "%" + name + "%");
        if (!isStringEmpty(description))
            query.setString("descriptionSearchTerm", "%" + description + "%");
        if (!isStringEmpty(mfgModel))
            query.setString("mfgModelSearchTerm", "%" + mfgModel + "%");
        if (!isStringEmpty(mfgName))
            query.setString("mfgNameSearchTerm", "%" + mfgName + "%");
        if (!isStringEmpty(mfgSerialNumber))
            query.setString("mfgSerialNumberSearchTerm", "%" + mfgSerialNumber + "%");
        logger.debug("Query string = " + query.getQueryString());
        devices = query.list();
        logger.debug("Found " + devices.size() + " devices from that query");
    } catch (HibernateException e) {
        throw new MetadataAccessException(e);
    }

    if (returnFullObjectGraph)
        devices = getRealObjectsAndRelationships(devices);

    return devices;
}

From source file:moos.ssds.dao.DeviceDAO.java

License:LGPL

/**
 * This method will take the search term supplied and return all devices
 * that have that term in any of the fields or in the device type name
 * /*from www.  ja v a 2 s . com*/
 * @param searchTerm
 * @param orderByPropertyName
 * @param ascendingOrDescending
 * @param returnFullObjectGraph
 * @return
 * @throws MetadataAccessException
 */
public Collection findBySearchingAllFieldsAndType(String searchTerm, String orderByPropertyName,
        String ascendingOrDescending, boolean returnFullObjectGraph) throws MetadataAccessException {
    // The Collection to return
    Collection devices = new ArrayList();

    // Formulate the query
    StringBuffer queryBuffer = new StringBuffer();
    queryBuffer.append("from Device device where " + "device.uuid like :searchTerm or "
            + "device.name like :searchTerm or " + "device.description like :searchTerm or "
            + "device.mfgModel like :searchTerm or " + "device.mfgName like :searchTerm or "
            + "device.mfgSerialNumber like :searchTerm or " + "device.infoUrlList like :searchTerm or "
            + "device.deviceType.name like :searchTerm or " + "device.person.surname like :searchTerm or "
            + "device.person.firstname like :searchTerm or " + "device.person.email like :searchTerm or "
            + "device.person.username like :searchTerm or " + "device.person.organization like :searchTerm");

    // Check for ordering criteria
    if (this.checkIfPropertyOK(orderByPropertyName)) {
        queryBuffer.append(this.getOrderByPropertyNameSQLClause(orderByPropertyName, ascendingOrDescending));
    }
    try {
        Query query = getSession().createQuery(queryBuffer.toString());
        query.setString("searchTerm", "%" + searchTerm + "%");
        logger.debug("Query string = " + query.getQueryString());
        devices = query.list();
        logger.debug("Found " + devices.size() + " devices from that query");
    } catch (HibernateException e) {
        throw new MetadataAccessException(e);
    }

    if (returnFullObjectGraph)
        devices = getRealObjectsAndRelationships(devices);

    return devices;
}

From source file:moos.ssds.dao.ResourceDAO.java

License:LGPL

/**
 * This method returns all <code>Resource</code>s that are of a certain
 * <code>ResourceType</code>
 *//*from w  w  w  .ja  va2s  . c  om*/
public Collection findByResourceType(ResourceType resourceType, String orderByPropertyName,
        String ascendingOrDescending, boolean returnFullObjectGraph) throws MetadataAccessException {

    // The collection to return
    Collection resources = new HashSet();

    // First validate the incoming deviceType
    if (resourceType == null) {
        return resources;
    }

    // If the resourceType already has an ID, use that, if not look up the
    // ID
    Long resourceTypeId = null;
    if ((resourceType.getId() != null) && (resourceType.getId().longValue() > 0)) {
        resourceTypeId = resourceType.getId();
    } else {
        // Get the ResourceTypeDAO and look up the ID
        ResourceTypeDAO resourceTypeDAO = new ResourceTypeDAO(getSession());
        resourceTypeId = resourceTypeDAO.findId(resourceType);
    }

    // If no ID was found, throw an exception
    if (resourceTypeId == null) {
        throw new MetadataAccessException(
                "No ResourceType was found in the data store" + " that matched the specified ResourceType");
    }

    // Construct the query and run it
    StringBuffer queryStringBuffer = new StringBuffer();
    queryStringBuffer.append("select distinct from Resource resource ");
    queryStringBuffer.append("where resource.resourceType.id = :resourceTypeId ");
    // Add order by clause
    if (this.checkIfPropertyOK(orderByPropertyName)) {
        queryStringBuffer
                .append(this.getOrderByPropertyNameSQLClause(orderByPropertyName, ascendingOrDescending));
    }
    try {
        Query query = getSession().createQuery(queryStringBuffer.toString());
        query.setString("resourceTypeId", resourceTypeId.toString());
        logger.debug("Compiled query = " + query.getQueryString());
        resources = query.list();
    } catch (HibernateException e) {
        throw new MetadataAccessException(e);
    }

    // Check for full object graphs
    if (returnFullObjectGraph)
        resources = getRealObjectsAndRelationships(resources);

    // Now return the results
    return resources;
}

From source file:moos.ssds.dao.StandardVariableDAO.java

License:LGPL

/**
 * This method looks up the <code>StandardVariable</code> that is associated
 * with a <code>RecordVariable</code>.
 * //from w  w w  .  j ava 2s  . c o  m
 * @param recordVariable
 *            is the <code>RecordVariable</code> to search for
 * @return is the <code>StandardVariable</code> that is linked to the given
 *         <code>RecordVariable</code>. Null will be returned if there is no
 *         association defined
 */
public StandardVariable findByRecordVariable(RecordVariable recordVariable) throws MetadataAccessException {

    // Create the StandardVariable to return
    StandardVariable standardVariableToReturn = null;

    // Construct query and run it
    if (recordVariable != null) {
        if (recordVariable.getId() == null) {
            throw new MetadataAccessException("StandardVariableDAO.findByRecordVariable: The incoming "
                    + "RecordVariable does not have an ID, one must exist to "
                    + "find the StandardVariable associated with the RecordVariable");
        }
        try {
            Query query = getSession().createQuery("select recordVariable.standardVariable from RecordVariable "
                    + "recordVariable where recordVariable.id = :recordVariableId");
            query.setString("recordVariableId", recordVariable.getId().toString());
            logger.debug("Compiled query = " + query.getQueryString());
            standardVariableToReturn = (StandardVariable) query.uniqueResult();
        } catch (HibernateException e) {
            throw new MetadataAccessException("HibernateException (" + e.getClass().getName()
                    + ") caught in StandardVariableDAO.findByRecordVariable (Message =  " + e.getMessage()
                    + ")");
        }
    }
    return standardVariableToReturn;
}