Example usage for org.hibernate StatelessSession createQuery

List of usage examples for org.hibernate StatelessSession createQuery

Introduction

In this page you can find the example usage for org.hibernate StatelessSession createQuery.

Prototype

@Override
    org.hibernate.query.Query createQuery(String queryString);

Source Link

Usage

From source file:org.squashtest.tm.service.internal.campaign.coercers.extenders.CampaignLibraryExtender.java

License:Open Source License

@Override
public Collection<? extends Serializable> doCoerce(Collection<? extends Serializable> ids) {
    StatelessSession s = hibernateStatelessSessionHelper.openStatelessSession();
    Transaction tx = s.beginTransaction();

    try {//from   w  ww  . ja v a  2s.  c  om
        Query q = s.createQuery(
                "select distinct l.id from CampaignLibrary l join l.rootContent c where c.id in (:clnIds)");
        q.setParameterList("clnIds", ids);
        return q.list();

    } finally {
        tx.commit();
        s.close();
    }
}

From source file:org.squashtest.tm.service.internal.campaign.coercers.extenders.CampaignLibraryNodePathEdgeExtender.java

License:Open Source License

@Override
public Collection<? extends Serializable> doCoerce(Collection<? extends Serializable> coercedIds) {
    StatelessSession s = hibernateStatelessSessionHelper.openStatelessSession();
    Transaction tx = s.beginTransaction();

    try {//from ww w .ja v a 2s.c om
        Query q = s.createQuery(
                "select distinct edge.ancestorId from CampaignPathEdge edge where edge.descendantId in (:clnIds) and depth=1");
        q.setParameterList("clnIds", coercedIds);
        coercedIds.addAll(q.list());
        return coercedIds;

    } finally {
        tx.commit();
        s.close();
    }
}

From source file:org.squashtest.tm.service.internal.campaign.coercers.extenders.TestSuiteToIterationExtender.java

License:Open Source License

@Override
public Collection<? extends Serializable> doCoerce(Collection<? extends Serializable> ids) {
    StatelessSession s = hibernateStatelessSessionHelper.openStatelessSession();
    Transaction tx = s.beginTransaction();

    try {/*from  ww w.  j  av  a 2  s  .  c om*/
        Query q = s.createQuery(
                "select distinct i.id from Iteration i join i.testSuites ts where ts.id in (:tsids)");
        q.setParameterList("tsids", ids);
        return q.list();

    } finally {
        tx.commit();
        s.close();
    }
}

From source file:org.squashtest.tm.service.internal.campaign.coercers.IterationToCampaignIdsCoercer.java

License:Open Source License

@Override
public Collection<? extends Serializable> coerce(Object ids) {
    StatelessSession s = hibernateStatelessSessionHelper.openStatelessSession();
    Transaction tx = s.beginTransaction();

    try {//from w  ww.  ja  va  2s  .  c  om
        Query q = s.createQuery(
                "select distinct c.id from Iteration i join i.campaign c where i.id in (:iterIds)");
        q.setParameterList("iterIds", (Collection<? extends Serializable>) ids);
        return q.list();

    } finally {
        tx.commit();
        s.close();
    }
}

From source file:org.squashtest.tm.service.internal.requirement.coercers.extenders.RequirementLibraryExtender.java

License:Open Source License

@Override
public Collection<? extends Serializable> doCoerce(Collection<? extends Serializable> ids) {
    StatelessSession s = hibernateStatelessSessionHelper.openStatelessSession();
    Transaction tx = s.beginTransaction();

    try {/*from   w w w.j av a 2  s . c o  m*/
        Query q = s.createQuery(
                "select distinct l.id from RequirementLibrary l join l.rootContent c where c.id in (:rlnIds) ");
        q.setParameterList("rlnIds", ids);
        return q.list();

    } finally {
        tx.commit();
        s.close();
    }
}

From source file:org.squashtest.tm.service.internal.requirement.coercers.extenders.RequirementLibraryNodePathEdgeExtender.java

License:Open Source License

@Override
public Collection<? extends Serializable> doCoerce(Collection<? extends Serializable> coercedIds) {
    StatelessSession s = hibernateStatelessSessionHelper.openStatelessSession();
    Transaction tx = s.beginTransaction();

    try {// w  w  w . j  a v  a2s. c  om
        Query q = s.createQuery(
                "select distinct edge.ancestorId from RequirementPathEdge edge where edge.descendantId in (:rlnIds) and depth=1");
        q.setParameterList("rlnIds", coercedIds);
        coercedIds.addAll(q.list());
        return coercedIds;

    } finally {
        tx.commit();
        s.close();
    }
}

From source file:org.squashtest.tm.service.internal.testcase.coercers.extenders.TestCaseLibraryExtender.java

License:Open Source License

@Override
public Collection<? extends Serializable> doCoerce(Collection<? extends Serializable> ids) {
    StatelessSession s = hibernateStatelessSessionHelper.openStatelessSession();
    Transaction tx = s.beginTransaction();

    try {//from  w  ww  .j av  a2  s .  c  om
        Query q = s.createQuery(
                "select distinct l.id from TestCaseLibrary l join l.rootContent c where c.id in (:tclnIds) ");
        q.setParameterList("tclnIds", ids);
        return q.list();

    } finally {
        tx.commit();
        s.close();
    }
}

From source file:org.squashtest.tm.service.internal.testcase.coercers.extenders.TestCaseLibraryNodePathEdgeExtender.java

License:Open Source License

@Override
public Collection<? extends Serializable> doCoerce(Collection<? extends Serializable> coercedIds) {
    StatelessSession s = hibernateStatelessSessionHelper.openStatelessSession();
    Transaction tx = s.beginTransaction();

    try {/*from  w  w w.  j  a va2 s  .  co  m*/
        Query q = s.createQuery(
                "select distinct edge.ancestorId from TestCasePathEdge edge where edge.descendantId in (:tclnIds) and depth=1");
        q.setParameterList("tclnIds", coercedIds);
        coercedIds.addAll(q.list());
        return coercedIds;

    } finally {
        tx.commit();
        s.close();
    }
}

From source file:vault.queryrouter.models.dao.JobQueueDAO.java

License:Apache License

public static TenantUpdateJob getPendingQueryLock(StatelessSession session, String workerId) throws Exception {

    try {//  www .  j ava  2  s. c  o  m
        session.getTransaction().begin();
        //Update the pending query(order by last_touch_timestamp) status from pending to processing
        String hql = "UPDATE TenantUpdateJob set status= '" + TenantUpdateJobStatus.Processing.toString()
                + "', workerId = '" + workerId + "' WHERE status='" + TenantUpdateJobStatus.Pending.toString()
                + "' AND type ='" + TenantUpdateJobType.queryrouter.toString()
                + "' ORDER BY lastTouchTime LIMIT 1";
        org.hibernate.Query query = session.createQuery(hql);
        int result = query.executeUpdate();
        session.getTransaction().commit();

        //Pending query exists and the status changes from "Pending" to "Processing"
        if (result != 0) {

            //Find the processing query by the thread(workerId)
            Criteria criteria = session.createCriteria(TenantUpdateJob.class);
            criteria.add(Expression.eq("workerId", workerId.toString()));
            criteria.add(Expression.eq("status", TenantUpdateJobStatus.Processing.toString()));
            TenantUpdateJob pendingQueueCandidate = (TenantUpdateJob) criteria.uniqueResult();

            Query q = QueryDAO.getQuery(session, pendingQueueCandidate.getQueryId());

            //Check the availability of the processing Query that could be actual processed
            //If the number of  conflictQuery is more than 0 which means the processing query conflicts with the another processing query(e.g.move_tenant_mppdb_data)
            int conflictQuery = JobQueueDAO.getProcessingQuery(session, q.getTenantMppdbId());

            //No conflict query -> Return the query(TenantUpdateJob Object)
            if (conflictQuery == 0) {

                return pendingQueueCandidate;
            }
            //Conflict query exists -> Status of the processing query turn back to "Pending" and the last_touch_timestamp will be updated
            else {

                try {
                    session.getTransaction().begin();
                    TenantUpdateJob jq = (TenantUpdateJob) session.get(TenantUpdateJob.class,
                            pendingQueueCandidate.getJobQueueId());
                    jq.setStatus(TenantUpdateJobStatus.Pending.toString());
                    jq.setWorkerId("");
                    session.update(jq);
                    session.getTransaction().commit();

                } catch (HibernateException e) {
                    logger.error("Exception: ", e);
                    if (session.getTransaction() != null)
                        session.getTransaction().rollback();
                    e.printStackTrace();
                }
                return null;
            }
        }
        //No pending query exists
        else {
            return null;
        }

    } catch (Exception e) {
        logger.error(e.toString());
        throw e;
    }
}

From source file:vault.queryrouter.models.dao.QueryDAO.java

License:Apache License

/**
 * Find the last statement of `SET search_path `. If no, find the schema......
 * @param session// ww w  .j ava  2 s .  c  o  m
 * @param connectionId
 * @return
 * @throws Exception
 */
public static String findSearchPath(StatelessSession session, String connectionId) throws Exception {
    try {

        String hql = "SELECT queryBody FROM Query WHERE SUBSTRING(LOWER(REPLACE(queryBody,' ','')),1,14)='setsearch_path' AND connectionId='"
                + connectionId + "' order by startTime asc";

        org.hibernate.Query query = session.createQuery(hql).setFirstResult(0);

        Transaction t = session.beginTransaction();
        List<String> queryLogs = query.list();
        t.commit();
        if (queryLogs.size() == 0)
            return null;
        else {
            return SQLRegexUtil.getSchemaFromSetSearchPath(queryLogs.get(0));
        }

    } catch (Exception e) {
        logger.error("Can't find search path", e);
        throw e;
    }
}