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:mystical.controllerDAO.AbstractDAO.java

protected List findAll(Class clazz) {
    session = Conexao.getSessionFactory().openSession();
    tx = session.beginTransaction();/*from  w ww  . j av a2  s  .  c  o m*/
    List objects = null;
    try {
        Query query = session.createQuery("from " + clazz.getName());
        System.out.print(query.getQueryString());
        objects = query.list();
        tx.commit();
        retorno = true;
    } catch (HibernateException e) {
        tx.rollback();
    } finally {
        session.close();
    }
    return objects;
}

From source file:net.sf.infrared.aspects.hibernate.Hibernate2Aspect.java

License:Apache License

public Object aroundQueryExecution(Query q, StaticJoinPoint sjp) throws Throwable {
    return aroundQueryExecution(q.getQueryString(), sjp);
}

From source file:nl.strohalm.cyclos.utils.ScrollableResultsIterator.java

License:Open Source License

public ScrollableResultsIterator(final Query query, final Transformer<Object[], T> transformer) {
    this.results = query.scroll(ScrollMode.FORWARD_ONLY);
    if (query instanceof SQLQuery) {
        // The getReturnTypes doesn't work for SQLQueries... Assume an array
        array = true;/* w  w  w .  j av  a2  s . co  m*/
    } else {
        // this (extra) check to see if the query starts with "select new" is just to support the
        // following case: SELECT new A(e.prop1, e.prop2) FROM Entity e ...
        // in that case we musn't return an array in the next() method.
        array = query.getReturnTypes().length > 1
                && !query.getQueryString().trim().toLowerCase().startsWith("select new");
    }
    this.transformer = transformer;
    getNextObject();

    DataIteratorHelper.registerOpen(this, true);
}

From source file:org.anyframe.iam.admin.securedresources.dao.impl.SecuredResourcesDaoHibernateImpl.java

License:Apache License

public Page getListwithLevel(ResourceSearchVO resourceSearchVO) throws Exception {
    int pageIndex = resourceSearchVO.getPage();
    int pageSize = resourceSearchVO.getRows();
    if (pageSize <= 0)
        pageSize = this.getPropertiesService().getInt("PAGE_SIZE");
    int pageUnit = this.getPropertiesService().getInt("PAGE_UNIT");
    String sidx = StringUtil.null2str(resourceSearchVO.getSidx());
    String sord = StringUtil.null2str(resourceSearchVO.getSord());
    String searchCondition = StringUtil.null2str(resourceSearchVO.getSearchCondition());
    String searchKeyword = StringUtil.null2str(resourceSearchVO.getSearchKeyword());
    String searchType = StringUtil.null2str(resourceSearchVO.getSearchtype());
    String roleId = StringUtil.null2str(resourceSearchVO.getRoleId());
    String systemName = StringUtil.null2str(resourceSearchVO.getSystemName());

    int startIndex = (pageIndex - 1) * pageSize;

    Query query = (Query) this.getSessionFactory().getCurrentSession()
            .getNamedQuery("findsecuredResourceswithlevel");
    StringBuffer replaceSQL = new StringBuffer();
    if (!("".equals(searchKeyword))) {
        if ("resourceName".equals(searchCondition)) {
            replaceSQL.append("AND a.resource_name like '%" + searchKeyword + "%'");
        } else if ("resourceId".equals(searchCondition)) {
            replaceSQL.append("AND a.resource_id like '%" + searchKeyword + "%'");
        } else {/* w  ww. ja  v  a  2  s. c  o  m*/
            replaceSQL.append("AND a.resource_pattern like '%" + searchKeyword + "%'");
        }
    }
    if ("URL".equals(searchType) || "Method".equals(searchType) || "PointCut".equals(searchType)) {
        replaceSQL.append("AND a.resource_type like '" + searchType + "'");
    }
    if ("".equals(systemName) || "All".equals(systemName)) {
        replaceSQL.append(" AND a.resource_name = " + systemName);
    }

    String queryString = query.getQueryString();
    queryString = queryString.replace("--replaceSQL1", replaceSQL.toString());

    StringBuffer replaceSQLOrderby = new StringBuffer();

    if ("resourceId".equals(sidx) || "".equals(sidx)) {
        replaceSQLOrderby.append("ORDER BY a.resource_id ");
    } else {
        replaceSQLOrderby.append("ORDER BY a." + sidx);
    }

    if ("desc".equals(sord) || "".equals(sord)) {
        replaceSQLOrderby.append("DESC");
    } else {
        replaceSQLOrderby.append("ASC");
    }
    queryString = queryString.replace("--replaceSQL2", replaceSQLOrderby.toString());

    SQLQuery replacedQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(queryString);
    replacedQuery.addEntity(IamResourceResult.class);
    replacedQuery.setParameter("roleId", roleId);
    replacedQuery.setFirstResult(startIndex);
    replacedQuery.setMaxResults(pageSize);

    @SuppressWarnings("unchecked")
    List<IamResourceResult> resultList = replacedQuery.list();

    query = (Query) this.getSessionFactory().getCurrentSession()
            .getNamedQuery("countsecuredResourceswithlevel");
    queryString = query.getQueryString();
    queryString = queryString.replace("--replaceSQL", replaceSQL.toString());
    replacedQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(queryString);
    replacedQuery.setParameter("roleId", roleId);

    int totalSize = Integer.parseInt(replacedQuery.list().get(0).toString());

    Page resultPage = new Page(resultList, pageIndex, totalSize, pageUnit, pageSize);
    return resultPage;
}

From source file:org.anyframe.iam.admin.viewresources.dao.impl.ViewResourcesDaoHibernateImpl.java

License:Apache License

@SuppressWarnings("unchecked")
public List<IamTree> getRootNodeOfViewsWithSystemName(String systemName) throws Exception {
    Query query = (Query) this.getSessionFactory().getCurrentSession().getNamedQuery("getRootNodeOfView");
    StringBuffer replaceSQL = new StringBuffer();

    if (!"".equals("systemName"))
        replaceSQL.append(" AND B.SYSTEM_NAME = '" + systemName + "'");

    String queryString = query.getQueryString();
    queryString = queryString.replace("--replaceSQL", replaceSQL.toString());

    SQLQuery replacedQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(queryString);
    replacedQuery.addEntity(IamTree.class);

    List<IamTree> resultList = replacedQuery.list();
    return resultList;
}

From source file:org.beanfuse.persist.hibernate.EntityDaoHibernate.java

License:Open Source License

/**
 * //from   w  ww .  ja  va2  s  . com
 * 
 * @param query
 * @return
 */
private String buildCountQueryStr(Query query) {
    String queryStr = "select count(*) ";
    if (query instanceof SQLQuery) {
        queryStr += "from (" + query.getQueryString() + ")";
    } else {
        String lowerCaseQueryStr = query.getQueryString().toLowerCase();
        String selectWhich = lowerCaseQueryStr.substring(0, query.getQueryString().indexOf("from"));
        int indexOfDistinct = selectWhich.indexOf("distinct");
        int indexOfFrom = lowerCaseQueryStr.indexOf("from");
        // ?distinct
        if (-1 != indexOfDistinct) {
            if (StringUtils.contains(selectWhich, ",")) {
                queryStr = "select count("
                        + query.getQueryString().substring(indexOfDistinct, query.getQueryString().indexOf(","))
                        + ")";

            } else {
                queryStr = "select count(" + query.getQueryString().substring(indexOfDistinct, indexOfFrom)
                        + ")";
            }
        }
        queryStr += query.getQueryString().substring(indexOfFrom);
    }
    return queryStr;
}

From source file:org.beangle.commons.orm.hibernate.BaseDaoHibernate.java

License:Open Source License

/**
 * //from  w  w  w.  j  a va2 s  .  c om
 * 
 * @param query
 * @return
 */
private String buildCountQueryStr(Query query) {
    String queryStr = "select count(*) ";
    if (query instanceof SQLQuery) {
        queryStr += "from (" + query.getQueryString() + ")";
    } else {
        String lowerCaseQueryStr = query.getQueryString().toLowerCase();
        String selectWhich = lowerCaseQueryStr.substring(0, query.getQueryString().indexOf("from"));
        int indexOfDistinct = selectWhich.indexOf("distinct");
        int indexOfFrom = lowerCaseQueryStr.indexOf("from");
        // ?distinct
        if (-1 != indexOfDistinct) {
            if (Strings.contains(selectWhich, ",")) {
                queryStr = "select count("
                        + query.getQueryString().substring(indexOfDistinct, query.getQueryString().indexOf(","))
                        + ")";

            } else {
                queryStr = "select count(" + query.getQueryString().substring(indexOfDistinct, indexOfFrom)
                        + ")";
            }
        }
        queryStr += query.getQueryString().substring(indexOfFrom);
    }
    return queryStr;
}

From source file:org.beangle.orm.hibernate.HibernateEntityDao.java

License:Open Source License

/**
 * /* ww w .ja  va  2s  .c  om*/
 * 
 * @param query
 * @return query string
 */
private String buildCountQueryStr(Query query) {
    String queryStr = "select count(*) ";
    if (query instanceof SQLQuery) {
        queryStr += "from (" + query.getQueryString() + ")";
    } else {
        String lowerCaseQueryStr = query.getQueryString().toLowerCase();
        String selectWhich = lowerCaseQueryStr.substring(0, query.getQueryString().indexOf("from"));
        int indexOfDistinct = selectWhich.indexOf("distinct");
        int indexOfFrom = lowerCaseQueryStr.indexOf("from");
        // ?distinct
        if (-1 != indexOfDistinct) {
            if (Strings.contains(selectWhich, ",")) {
                queryStr = "select count("
                        + query.getQueryString().substring(indexOfDistinct, query.getQueryString().indexOf(","))
                        + ")";
            } else {
                queryStr = "select count(" + query.getQueryString().substring(indexOfDistinct, indexOfFrom)
                        + ")";
            }
        }
        queryStr += query.getQueryString().substring(indexOfFrom);
    }
    return queryStr;
}

From source file:org.bonitasoft.engine.persistence.AbstractHibernatePersistenceService.java

License:Open Source License

@Override
public <T> List<T> selectList(final SelectListDescriptor<T> selectDescriptor) throws SBonitaReadException {
    try {/*w  w w  .j  a v  a 2  s.com*/
        final Class<? extends PersistentObject> entityClass = selectDescriptor.getEntityType();
        checkClassMapping(entityClass);

        final Session session = getSession(true);
        Query query = session.getNamedQuery(selectDescriptor.getQueryName());
        String builtQuery = query.getQueryString();

        if (selectDescriptor.hasAFilter()) {
            final QueryOptions queryOptions = selectDescriptor.getQueryOptions();
            final boolean enableWordSearch = isWordSearchEnabled(selectDescriptor.getEntityType());
            builtQuery = getQueryWithFilters(builtQuery, queryOptions.getFilters(),
                    queryOptions.getMultipleFilter(), enableWordSearch);
        }

        if (selectDescriptor.hasOrderByParameters()) {
            builtQuery = getQueryWithOrderByClause(builtQuery, selectDescriptor);
        }

        if (!builtQuery.equals(query.getQueryString())) {
            query = session.createQuery(builtQuery);
        }
        setQueryCache(query, selectDescriptor.getQueryName());
        setParameters(query, selectDescriptor.getInputParameters());
        query.setFirstResult(selectDescriptor.getStartIndex());
        query.setMaxResults(selectDescriptor.getPageSize());

        checkOrderByClause(query);

        @SuppressWarnings("unchecked")
        final List<T> list = query.list();
        if (list != null) {
            return list;
        }
        return Collections.emptyList();
    } catch (final AssertionFailure | LockAcquisitionException | StaleStateException e) {
        throw new SRetryableException(e);
    } catch (final HibernateException | SPersistenceException e) {
        throw new SBonitaReadException(e, selectDescriptor);
    }
}

From source file:org.bonitasoft.engine.persistence.AbstractHibernatePersistenceService.java

License:Open Source License

private void checkOrderByClause(final Query query) {
    if (!query.getQueryString().toLowerCase().contains("order by")) {
        switch (orderByCheckingMode) {
        case NONE:
            break;
        case WARNING:
            logger.log(AbstractHibernatePersistenceService.class, TechnicalLogSeverity.WARNING, "Query '"
                    + query.getQueryString()
                    + "' does not contain 'ORDER BY' clause. It's better to modify your query to order the result, especially if you use the pagination.");
            break;
        case STRICT:
        default:/*from   w  w w.  j  av a  2  s  .co  m*/
            throw new IllegalArgumentException("Query " + query.getQueryString()
                    + " does not contain 'ORDER BY' clause hence is not allowed. Please specify ordering before re-sending the query");
        }
    }
}