Example usage for org.hibernate SQLQuery setString

List of usage examples for org.hibernate SQLQuery setString

Introduction

In this page you can find the example usage for org.hibernate SQLQuery setString.

Prototype

@Deprecated
@SuppressWarnings("unchecked")
default Query<R> setString(int position, String val) 

Source Link

Document

Bind a positional String-valued parameter.

Usage

From source file:org.openbravo.test.dal.IssuesTest.java

License:Open Source License

/**
   * Testing issue 0017058. It verifies that the NVARCHAR JDBC type is properly mapped The test SQL
   * query is used in the IDL module./* w w  w.ja  v  a2  s  .  c  o  m*/
   */
  @Test
  public void test17058() {

      setSystemAdministratorContext();

      final Session session = OBDal.getInstance().getSession();
      SQLQuery query = session.createSQLQuery(
              "SELECT AD_REF_LIST.VALUE AS VALUE, AD_REF_LIST.NAME AS LISTNAME, TRL.NAME AS TRLNAME "
                      + "FROM AD_REF_LIST LEFT OUTER JOIN "
                      + "(SELECT AD_REF_LIST_ID, NAME FROM AD_REF_LIST_TRL WHERE AD_REF_LIST_TRL.AD_LANGUAGE = ?) TRL "
                      + "ON AD_REF_LIST.AD_REF_LIST_ID = TRL.AD_REF_LIST_ID "
                      + "WHERE AD_REF_LIST.AD_REFERENCE_ID = ?");
      query.setString(0, "en_US");
      query.setString(1, "800025");

      @SuppressWarnings("unchecked")
      java.util.List<Object[]> l = query.list();

  }

From source file:org.openiam.idm.srvc.qry.service.QueryDataServiceImpl.java

License:Open Source License

public List<Object> executeQuery(Query qry) {

    Session session = sessionFactory.getCurrentSession();

    SQLQuery sqlQry = session.createSQLQuery(qry.getSql());
    sqlQry.addEntity(qry.getObjectClass());

    // add the parameters for the query if they exists
    List<QueryParam> paramList = qry.getParamList();
    if (paramList != null && paramList.size() > 0) {
        for (QueryParam param : paramList) {
            if (param.getParamType() == ParameterTypeEnum.STRING) {
                System.out.println(" - Setting parameters in query service.");
                sqlQry.setString(param.getParamName(), (String) param.getParamValue());
            }// w w w.  ja  v  a2 s .co  m
        }
    }

    if (qry.getMaxRowCount() != 0) {
        sqlQry.setFetchSize(qry.getMaxRowCount());
        sqlQry.setMaxResults(qry.getMaxRowCount());
    }
    return sqlQry.list();

}

From source file:org.openmrs.module.complexdatadb.api.db.hibernate.HibernateComplexDataToDBDAO.java

License:Open Source License

/**
  * @see ComplexDataToDBDAO#getComplexDataToDB()
  *//*from w w w .j ava2  s. c  om*/
@Override
public ComplexDataToDB getComplexDataToDB(String uuid) {
    Session session = sessionFactory.getCurrentSession();
    SQLQuery query = session.createSQLQuery("select * from obs_complex_data where uuid = :uuid")
            .addEntity(ComplexDataToDB.class);
    ComplexDataToDB data = (ComplexDataToDB) query.setString("uuid", uuid).list().get(0);

    return data;
}

From source file:org.openmrs.module.encounteraudit.api.db.hibernate.HibernateEncounterAuditDAO.java

License:Open Source License

@Override
public List<Encounter> getAuditEncounters(Date fromDate, Date toDate, int sampleSize, Location location,
        EncounterType encounterType, String creatorId) {

    if (sampleSize < 1) {
        // by default return 25 records
        sampleSize = 25;//from ww w . j  ava  2  s . c om
    }

    StringBuilder sql = new StringBuilder("select * from encounter e where ");
    sql.append(" encounter_datetime > :fromDate and ");
    sql.append(" encounter_datetime < :toDate ");
    if (location != null) {
        sql.append(" and location_id = :locationId ");
    }
    if (encounterType != null) {
        sql.append(" and encounter_type = :encounterType ");
    }
    if (creatorId.length() != 0) {
        sql.append(" and creator = :creatorId and changed_by IS NULL ");
    }
    sql.append(" order by rand() ");
    sql.append("limit 0,:sampleSize ");

    SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(sql.toString())
            .addEntity(Encounter.class);
    query.setDate("fromDate", fromDate);
    query.setDate("toDate", toDate);
    query.setInteger("sampleSize", new Integer(sampleSize));

    if (location != null) {
        query.setInteger("locationId", new Integer(location.getLocationId()));
    }
    if (encounterType != null) {
        query.setInteger("encounterType", new Integer(encounterType.getEncounterTypeId()));
    }
    if (creatorId.length() != 0) {
        query.setString("creatorId", creatorId);
    }

    List<Encounter> encounterList = query.list();

    return encounterList;
}

From source file:org.openxdata.server.dao.hibernate.HibernateFormDownloadDAO.java

License:Apache License

/**
 * Gets form version xml as returned by a given sql statement.
 * /*  w w  w  . j  a  v  a  2 s .  c  o m*/
 * @param sql the sql statement.
 * @return the map of form versions xforms xml keyed by form version id.
 */
@SuppressWarnings("unchecked")
private Map<Integer, String> getFormsVersionXml(User user, boolean defaultForms, Integer studyId) {

    Map<Integer, String> forms = new LinkedHashMap<Integer, String>();
    List<Object[]> formList = null;

    Session session = getSessionFactory().getCurrentSession();

    if (user == null || user.hasAdministrativePrivileges()) {
        // existing query
        if (log.isDebugEnabled() && user != null) {
            log.debug("User " + user.getName() + " is an administrator, so all studies will be loaded");
        }
        String sql = "select form_definition_version_id,xform from form_definition_version fdv"
                + " inner join form_definition fd on fd.form_definition_id=fdv.form_definition_id"
                + " where xform is not null" + (studyId != null ? " and fd.study_id=" + studyId : "")
                + (defaultForms ? " and is_default=1" : "") + " order by fd.name";
        SQLQuery query = session.createSQLQuery(sql);
        query.addScalar("form_definition_version_id", Hibernate.INTEGER);
        query.addScalar("xform", Hibernate.STRING);
        formList = query.list();
    } else {
        // new query
        Query query = session.createQuery(
                "select distinct fdv.formDefVersionId, fdv.xform, fdv.formDef.name from FormDefVersion as fdv, User u"
                        + " where u.name = :name" + (defaultForms ? " and fdv.isDefault = :default" : "")
                        + (studyId != null ? " and fdv.formDef.study.studyId = :studyId" : "")
                        + " and (u in elements(fdv.formDef.users) or u in elements(fdv.formDef.study.users))"
                        + " order by fdv.formDef.name");
        query.setString("name", user.getName());
        query.setBoolean("default", defaultForms);
        if (studyId != null)
            query.setInteger("studyId", studyId);
        formList = query.list();
    }

    // process results
    for (Object[] form : formList) {
        String xform = (String) form[1];
        if (xform != null && xform.trim().length() > 0) {
            forms.put((Integer) form[0], xform);
        } else {
            log.info("Did not load xform with id " + form[0] + " due to empty xform");
        }
    }

    return forms;
}

From source file:org.theospi.portfolio.presentation.model.impl.PresentationManagerImpl.java

License:Educational Community License

public List getPresentationComments(Id presentationId, Agent viewer) {
    Session session = getSession();//from   www .  j a v  a  2 s . c  o m

    SQLQuery query = session.createSQLQuery("SELECT {osp_presentation_comment.*} "
            + " FROM osp_presentation_comment {osp_presentation_comment}, osp_presentation p "
            + " WHERE {osp_presentation_comment}.presentation_id = p.id and p.id = :presentationId and"
            + " (visibility = " + PresentationComment.VISABILITY_PUBLIC + " or " + "   (visibility = "
            + PresentationComment.VISABILITY_SHARED + " and " + "    p.owner_id = :viewerId) or "
            + " creator_id = :viewerId)" + " ORDER BY {osp_presentation_comment}.created");

    query.addEntity("osp_presentation_comment", PresentationComment.class);
    query.setString("presentationId", presentationId.getValue());
    query.setString("viewerId", viewer.getId().getValue());

    try {
        return query.list();
    } catch (HibernateException e) {
        logger.error("", e);
        throw new OspException(e);
    }
}

From source file:org.theospi.portfolio.presentation.model.impl.PresentationManagerImpl.java

License:Educational Community License

public List getOwnerComments(Agent owner, CommentSortBy sortBy, boolean excludeOwner) {
    String orderBy = sortBy.getSortByColumn();

    if (orderBy.startsWith("owner_id") || orderBy.startsWith("name")) {
        orderBy = "p." + orderBy;
    } else {//  ww  w .  ja v a  2 s.  c om
        orderBy = "{osp_presentation_comment}." + orderBy;
    }

    Session session = getSession();

    String includeOwnerCondition = "";
    if (!excludeOwner) {
        includeOwnerCondition = " or creator_id = :ownerId ) ";
    } else {
        includeOwnerCondition = " ) and ( creator_id != :ownerId )";
    }

    SQLQuery query = session.createSQLQuery("SELECT {osp_presentation_comment.*} "
            + " FROM osp_presentation_comment {osp_presentation_comment}, osp_presentation p "
            + " WHERE {osp_presentation_comment}.presentation_id = p.id and " + " (visibility = "
            + PresentationComment.VISABILITY_PUBLIC + " or " + "  visibility = "
            + PresentationComment.VISABILITY_SHARED + includeOwnerCondition + " and "
            + "  p.owner_id = :ownerId " + " ORDER BY " + orderBy + " " + sortBy.getDirection());

    query.addEntity("osp_presentation_comment", PresentationComment.class);
    query.setString("ownerId", owner.getId().getValue());

    try {
        return query.list();
    } catch (HibernateException e) {
        logger.error("", e);
        throw new OspException(e);
    }
}

From source file:org.theospi.portfolio.presentation.model.impl.PresentationManagerImpl.java

License:Educational Community License

public List getOwnerComments(Agent owner, String toolId, CommentSortBy sortBy, boolean excludeOwner) {
    String orderBy = sortBy.getSortByColumn();

    if (orderBy.startsWith("owner_id") || orderBy.startsWith("name")) {
        orderBy = "p." + orderBy;
    } else {//from  w  w  w.j  a v  a 2  s  .c o  m
        orderBy = "{osp_presentation_comment}." + orderBy;
    }

    Session session = getSession();
    String includeOwnerCondition = "";
    if (!excludeOwner) {
        includeOwnerCondition = " or creator_id = :ownerId ) ";
    } else {
        includeOwnerCondition = " ) and ( creator_id != :ownerId )";
    }

    StringBuilder queryBuf = new StringBuilder("SELECT {osp_presentation_comment.*} "
            + " FROM osp_presentation_comment {osp_presentation_comment}, osp_presentation p "
            + " WHERE {osp_presentation_comment}.presentation_id = p.id and ");

    if (!isOnWorkspaceTab()) {
        queryBuf.append(" p.tool_id = :toolId and ");
    }

    queryBuf.append(" (visibility = " + PresentationComment.VISABILITY_PUBLIC + " or ");
    queryBuf.append("  visibility = " + PresentationComment.VISABILITY_SHARED);
    queryBuf.append(includeOwnerCondition + " and ");
    queryBuf.append("  p.owner_id = :ownerId ");
    queryBuf.append(" ORDER BY " + orderBy + " " + sortBy.getDirection());

    SQLQuery query = session.createSQLQuery(queryBuf.toString());

    query.addEntity("osp_presentation_comment", PresentationComment.class);
    if (!isOnWorkspaceTab()) {
        query.setString("toolId", toolId);
    }
    query.setString("ownerId", owner.getId().getValue());

    try {
        return query.list();
    } catch (HibernateException e) {
        logger.error("", e);
        throw new OspException(e);
    }
}

From source file:org.theospi.portfolio.presentation.model.impl.PresentationManagerImpl.java

License:Educational Community License

public List getCreatorComments(Agent creator, CommentSortBy sortBy) {
    String orderBy = sortBy.getSortByColumn();

    if (orderBy.startsWith("owner_id") || orderBy.startsWith("name")) {
        orderBy = "p." + orderBy;
    } else {/*from ww w  .  ja  v a2s .  c o  m*/
        orderBy = "{osp_presentation_comment}." + orderBy;
    }

    String queryString = "SELECT {osp_presentation_comment.*} "
            + " FROM osp_presentation_comment {osp_presentation_comment}, osp_presentation p "
            + " WHERE {osp_presentation_comment}.presentation_id = p.id and " + " creator_id = :creatorId"
            + " ORDER BY " + orderBy + " " + sortBy.getDirection();

    Session session = getSession();

    SQLQuery query = session.createSQLQuery(queryString);

    query.addEntity("osp_presentation_comment", PresentationComment.class);
    query.setString("creatorId", creator.getId().getValue());

    try {
        return query.list();
    } catch (HibernateException e) {
        logger.error("", e);
        throw new OspException(e);
    }
}

From source file:org.theospi.portfolio.presentation.model.impl.PresentationManagerImpl.java

License:Educational Community License

public List getCreatorComments(Agent creator, String toolId, CommentSortBy sortBy) {
    String orderBy = sortBy.getSortByColumn();

    if (orderBy.startsWith("owner_id") || orderBy.startsWith("name")) {
        orderBy = "p." + orderBy;
    } else {//from  ww w  .j ava 2s . co  m
        orderBy = "{osp_presentation_comment}." + orderBy;
    }

    StringBuilder queryBuf = new StringBuilder("SELECT {osp_presentation_comment.*} "
            + " FROM osp_presentation_comment {osp_presentation_comment}, osp_presentation p "
            + " WHERE {osp_presentation_comment}.presentation_id = p.id and ");

    if (!isOnWorkspaceTab()) {
        queryBuf.append(" tool_id = :toolId and");
    }
    queryBuf.append(" creator_id = :creatorId");
    queryBuf.append(" ORDER BY " + orderBy + " " + sortBy.getDirection());

    Session session = getSession();

    SQLQuery query = session.createSQLQuery(queryBuf.toString());

    query.addEntity("osp_presentation_comment", PresentationComment.class);
    if (!isOnWorkspaceTab()) {
        query.setString("toolId", toolId);
    }
    query.setString("creatorId", creator.getId().getValue());

    try {
        return query.list();
    } catch (HibernateException e) {
        logger.error("", e);
        throw new OspException(e);
    }
}