Example usage for org.hibernate Session getNamedQuery

List of usage examples for org.hibernate Session getNamedQuery

Introduction

In this page you can find the example usage for org.hibernate Session getNamedQuery.

Prototype

org.hibernate.Query getNamedQuery(String queryName);

Source Link

Document

Create a Query instance for the named query.

Usage

From source file:com.evolveum.midpoint.repo.sql.SqlRepositoryServiceImpl.java

License:Apache License

private <T extends ShadowType> List<PrismObject<T>> listResourceObjectShadowsAttempt(String resourceOid,
        Class<T> resourceObjectShadowType, OperationResult result)
        throws ObjectNotFoundException, SchemaException {

    List<PrismObject<T>> list = new ArrayList<>();
    Session session = null;
    try {/*  w  ww.ja  v a  2s.c om*/
        session = beginReadOnlyTransaction();
        Query query = session.getNamedQuery("listResourceObjectShadows");
        query.setString("oid", resourceOid);
        query.setResultTransformer(GetObjectResult.RESULT_TRANSFORMER);

        List<GetObjectResult> shadows = query.list();
        LOGGER.debug("Query returned {} shadows, transforming to JAXB types.",
                new Object[] { (shadows != null ? shadows.size() : 0) });

        if (shadows != null) {
            for (GetObjectResult shadow : shadows) {
                PrismObject<T> prismObject = updateLoadedObject(shadow, resourceObjectShadowType, null,
                        session);
                list.add(prismObject);
            }
        }
        session.getTransaction().commit();
    } catch (SchemaException | RuntimeException ex) {
        handleGeneralException(ex, session, result);
    } finally {
        cleanupSessionAndResult(session, result);
    }

    return list;
}

From source file:com.evolveum.midpoint.repo.sql.SqlRepositoryServiceImpl.java

License:Apache License

private <T extends ObjectType> String getVersionAttempt(Class<T> type, String oid, OperationResult result)
        throws ObjectNotFoundException, SchemaException {
    String version = null;/*from   www . j  av a2s  .  c o m*/
    Session session = null;
    try {
        session = beginReadOnlyTransaction();
        Query query = session.getNamedQuery("getVersion");
        query.setString("oid", oid);

        Number versionLong = (Number) query.uniqueResult();
        if (versionLong == null) {
            throw new ObjectNotFoundException(
                    "Object '" + type.getSimpleName() + "' with oid '" + oid + "' was not found.");
        }
        version = versionLong.toString();
    } catch (RuntimeException ex) {
        handleGeneralRuntimeException(ex, session, result);
    } finally {
        cleanupSessionAndResult(session, result);
    }

    return version;
}

From source file:com.evolveum.midpoint.repo.sql.SqlRepositoryServiceImpl.java

License:Apache License

private boolean isAnySubordinateAttempt(String upperOrgOid, Collection<String> lowerObjectOids) {
    Session session = null;
    try {//ww w  .j  a v  a 2 s  .  com
        session = beginTransaction();

        Query query;
        if (lowerObjectOids.size() == 1) {
            query = session.getNamedQuery("isAnySubordinateAttempt.oneLowerOid");
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append("select count(*) from ROrgClosure o where ");

            sb.append('(');
            Iterator<String> iterator = lowerObjectOids.iterator();
            int paramIndex = 0;
            while (iterator.hasNext()) {
                iterator.next();
                sb.append("(o.ancestorOid=:aOid").append(paramIndex);
                sb.append(" and o.descendantOid=:dOid").append(paramIndex);
                sb.append(')');
                paramIndex++;
                if (iterator.hasNext()) {
                    sb.append(" or ");
                }
            }
            sb.append(')');

            query = session.createQuery(sb.toString());
        }

        Iterator<String> iterator = lowerObjectOids.iterator();
        int paramIndex = 0;
        while (iterator.hasNext()) {
            String subOid = iterator.next();
            query.setString("aOid" + paramIndex, upperOrgOid);
            query.setString("dOid" + paramIndex, subOid);
            paramIndex++;
        }

        Number number = (Number) query.uniqueResult();
        return (number != null && number.longValue() != 0L) ? true : false;
    } catch (RuntimeException ex) {
        handleGeneralException(ex, session, null);
    } finally {
        cleanupSessionAndResult(session, null);
    }

    throw new SystemException("isAnySubordinateAttempt failed somehow, this really should not happen.");
}

From source file:com.examples.hql.NamedQueriesExamples.java

public static void main(String[] args) {

    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session session = sf.openSession();

    try {/*ww w  . j a  va 2 s.  c  om*/

        Query q = session.getNamedQuery("HQL_GET_ALL_PURCHASEORDERS");

        List<PurchaseOrderHeader> listPOH = q.list();

        for (PurchaseOrderHeader poh : listPOH) {

            System.out.println(poh.getPoheaderId() + " | " + poh.getPonumber() + " | " + poh.getPodate() + " | "
                    + poh.getOrderValue());
        }

        System.out.println("----------***********-------------");

        Query q1 = session.getNamedQuery("HQL_GET_PURCHASEORDER_BY_ID");
        q1.setParameter("id", 1);
        PurchaseOrderHeader poh = (PurchaseOrderHeader) q1.uniqueResult();

        System.out.println(poh.getPoheaderId() + " | " + poh.getPonumber() + " | " + poh.getPodate() + " | "
                + poh.getOrderValue());

        System.out.println("----------***********-------------");

        Query q2 = session.getNamedQuery("HQL_GET_PURCHASEORDER_BY_PONUMBER");
        q2.setParameter("ponumber", "PO102030");
        PurchaseOrderHeader poh1 = (PurchaseOrderHeader) q2.uniqueResult();

        System.out.println(poh1.getPoheaderId() + " | " + poh1.getPonumber() + " | " + poh1.getPodate() + " | "
                + poh1.getOrderValue());

        System.out.println("----------***********-------------");

        session.close();
        sf.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:com.floreantpos.model.dao._BaseRootDAO.java

License:Open Source License

/**
 * Obtain an instance of Query for a named query string defined in the mapping file.
 * Use the session given.//from  ww w  .  j av  a  2s  .c  o  m
 * @param name the name of a query defined externally 
 * @param s the Session
 * @return Query
 */
protected Query getNamedQuery(String name, Session s) {
    Query q = s.getNamedQuery(name);
    return q;
}

From source file:com.floreantpos.model.dao._BaseRootDAO.java

License:Open Source License

/**
 * Obtain an instance of Query for a named query string defined in the mapping file.
 * Use the session given./*from   w  w w  .ja v  a 2  s .  co  m*/
 * @param name the name of a query defined externally 
 * @param param the first parameter to set
 * @param s the Session
 * @return Query
 */
protected Query getNamedQuery(String name, Serializable param, Session s) {
    Query q = s.getNamedQuery(name);
    q.setParameter(0, param);
    return q;
}

From source file:com.floreantpos.model.dao._BaseRootDAO.java

License:Open Source License

/**
 * Obtain an instance of Query for a named query string defined in the mapping file.
 * Use the parameters given and the Session given.
 * @param name the name of a query defined externally 
 * @param params the parameter array//from  www.  j  a  v a  2 s .co  m
 * @s the Session
 * @return Query
 */
protected Query getNamedQuery(String name, Serializable[] params, Session s) {
    Query q = s.getNamedQuery(name);
    if (null != params) {
        for (int i = 0; i < params.length; i++) {
            q.setParameter(i, params[i]);
        }
    }
    return q;
}

From source file:com.floreantpos.model.dao._BaseRootDAO.java

License:Open Source License

/**
 * Obtain an instance of Query for a named query string defined in the mapping file.
 * Use the parameters given and the Session given.
 * @param name the name of a query defined externally 
 * @param params the parameter Map/*  w w w .  j  a v  a  2  s .  com*/
 * @s the Session
 * @return Query
 */
protected Query getNamedQuery(String name, Map params, Session s) {
    Query q = s.getNamedQuery(name);
    if (null != params) {
        for (Iterator i = params.entrySet().iterator(); i.hasNext();) {
            Map.Entry entry = (Map.Entry) i.next();
            q.setParameter((String) entry.getKey(), entry.getValue());
        }
    }
    return q;
}

From source file:com.fordoctor.sqlimpl.HibernateUtil.java

public static List<?> getListBy_Date(String date) {

    Session ses = getSessionFactory().openSession();
    //        ses.createQuery("Seanse.findBysDate",date);
    ses.beginTransaction();/*  www .j  av  a  2 s.  co  m*/
    Query query = ses.getNamedQuery("Seanse.findBySDate");
    query.setString("sDate", date);
    List<Seanse> res = query.list();

    ses.getTransaction().commit();
    ses.flush();
    try {
        ses.close();
    } catch (HibernateException he) {
        JOptionPane.showMessageDialog(null, he.getMessage());
    }

    return res;
}

From source file:com.fordoctor.sqlimpl.HibernateUtil.java

public List<?> getListByName(String name) {
    Session ses = getSessionFactory().openSession();
    ses.beginTransaction();/* w  w w  .  ja v  a2 s  .  c o m*/
    Query query = ses.getNamedQuery("Seanse.findByPacientName");
    query.setString("pacientName", name);
    List<Seanse> res = query.list();
    ses.getTransaction().commit();
    try {
        ses.close();
    } catch (HibernateException he) {

    }
    return res;
}