Example usage for org.hibernate SQLQuery list

List of usage examples for org.hibernate SQLQuery list

Introduction

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

Prototype

List<R> list();

Source Link

Document

Return the query results as a List.

Usage

From source file:com.hrms.manager.GasManager.java

public List<ResidentialQuarters> listEdit() {
    Session s = HibernateUtil.getSessionFactory().openSession();
    Transaction t = s.beginTransaction();
    List l = null;//from   ww  w .j  a  v a2s .co  m
    String sql = null;
    sql = "select r.consumer_number from gas_connection_master r where r.connection_id not in (select connection_id from gas_allotment)";
    SQLQuery query = s.createSQLQuery(sql);
    //  query.setParameter("quartertype",quartertype);
    l = query.list();
    System.out.println(l);
    return l;
}

From source file:com.hrms.manager.QuarterManager.java

public List quarterCode(Quarterdto emp) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = session.beginTransaction();
    List quartercode;/*from   ww w  .ja va 2s.c  o m*/
    int empId = emp.getEmpId();
    String sql = null;
    String quarterType = null;
    HashMap<Integer, String> dept = new HashMap<>();
    List<EmployeeProfile> departments = (List<EmployeeProfile>) session.createCriteria(EmployeeProfile.class)
            .list();
    for (EmployeeProfile d : departments) {
        dept.put(d.getEmpId(), d.getQuarterType());
    }
    Iterator it = dept.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<Integer, String> pair = (Map.Entry) it.next();
        //  System.out.println(pair.getKey()+ pair.getValue());
        if (empId == pair.getKey()) {
            quarterType = pair.getValue();
            System.out.println(quarterType);
        }
        it.remove();
    }
    List<QuarterAllotment> list1 = null;
    List<QuarterAllotment> list = (List<QuarterAllotment>) session.createCriteria(QuarterAllotment.class)
            .list();
    if (list.isEmpty()) {
        //         Criteria cr = session.createCriteria(ResidentialQuarters.class)
        //    .setProjection(Projections.projectionList()
        //             .add(Projections.property("quarterCode"), "quarterCode")).add(Restrictions.eq("quarterType","quarterType"));
        //      
        sql = "select quarter_code from residential_quarters where quarter_type=:quarterType";
        SQLQuery query = session.createSQLQuery(sql);
        query.setParameter("quarterType", quarterType);
        quartercode = query.list();
        list1 = query.list();
        System.out.println(list1);
        return list1;
    } else
        sql = "select distinct(r.quarter_code) from residential_quarters r,quarter_allotment q where r.quarter_type=:quarterType and r.quarter_id not in (select quarter_id from quarter_allotment)";
    SQLQuery query = session.createSQLQuery(sql);
    query.setParameter("quarterType", quarterType);
    quartercode = query.list();

    //     System.out.println(quartercode);
    return quartercode;
}

From source file:com.hrms.manager.QuarterManager.java

public List<ResidentialQuarters> listEdit() {
    Session s = HibernateUtil.getSessionFactory().openSession();
    Transaction t = s.beginTransaction();
    List l = null;/*from ww  w  .  ja  v a  2 s.  c om*/
    String sql = null;
    sql = "select r.quarter_code from residential_quarters r where r.quarter_id not in (select quarter_id from quarter_allotment)";
    SQLQuery query = s.createSQLQuery(sql);
    //  query.setParameter("quartertype",quartertype);
    l = query.list();
    System.out.println(l);
    return l;
}

From source file:com.huateng.scf.systemmng.dao.SCFHQLDAO.java

/**
 * ??//from w w w  .java2  s.  c  om
 *
 * @param sqlSQL?
 * @return Iterator?
 * @throws CommonException
 */
public Iterator queryBySQL(String sql) throws CommonException {
    if (logger.isDebugEnabled()) {
        logger.debug("queryBySQL(String) - start"); //$NON-NLS-1$
    }
    final String tempSql = sql;
    Iterator it = null;
    try {
        it = (Iterator) this.getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                if (logger.isDebugEnabled()) {
                    logger.debug("queryBySQL(String) - sql sql=" + tempSql); //$NON-NLS-1$
                }
                SQLQuery sqlQuery = session.createSQLQuery(tempSql);
                return sqlQuery.list().iterator();
            }
        });
        if (logger.isDebugEnabled()) {
            logger.debug("queryBySQL(String) - list end"); //$NON-NLS-1$
        }
    } catch (Exception e) {
        logger.error("queryBySQL(String)", e); //$NON-NLS-1$
        ExceptionUtil.throwCommonException(e.getMessage());
    }

    if (logger.isDebugEnabled()) {
        logger.debug("queryBySQL(String) - end"); //$NON-NLS-1$
    }
    return it;
}

From source file:com.ibm.ioes.utilities.AjaxHelper.java

public ArrayList getUsersOfRole(String roleId) throws Exception {
    ArrayList employeeList = new ArrayList();
    Session hibernateSession = null;/* www  . j  ava2  s . c  om*/

    try {
        hibernateSession = com.ibm.ioes.npd.hibernate.dao.CommonBaseDao.beginTrans();
        String sql = "SELECT   TM_EMPLOYEE.NPDEMPID,TM_EMPLOYEE.EMPNAME FROM   NPD.TM_ROLEEMPMAPPING TM_ROLEEMPMAPPING "
                + "INNER JOIN NPD.TM_ROLES TM_ROLES ON TM_ROLEEMPMAPPING.ROLEID = TM_ROLES.ROLEID"
                + "   INNER JOIN NPD.TM_EMPLOYEE TM_EMPLOYEE "
                + "ON TM_ROLEEMPMAPPING.NPDEMPID = TM_EMPLOYEE.NPDEMPID WHERE TM_ROLES.ROLEID=:roleId";
        SQLQuery query = hibernateSession.createSQLQuery(sql);

        query.setLong("roleId", Long.valueOf(roleId));

        query.addScalar("npdempid", Hibernate.LONG).addScalar("empname", Hibernate.STRING)
                .setResultTransformer(Transformers.aliasToBean(TmEmployee.class));

        employeeList = (ArrayList) query.list();

    } catch (Exception ex) {
        ex.printStackTrace();
        String msg = ex.getMessage() + " Exception occured in getUsersOfRole method of "
                + this.getClass().getSimpleName() + AppUtility.getStackTrace(ex);
        AppConstants.NPDLOGGER.error(msg);
        throw new NpdException(msg);
    } finally {
        hibernateSession.close();
    }
    return employeeList;
}

From source file:com.impetus.client.rdbms.HibernateClient.java

License:Apache License

@Override
public <E> List<E> getColumnsById(String schemaName, String joinTableName, String joinColumnName,
        String inverseJoinColumnName, Object parentId, Class columnJavaType) {
    StringBuffer sqlQuery = new StringBuffer();
    sqlQuery.append("SELECT ").append(inverseJoinColumnName).append(" FROM ")
            .append(getFromClause(schemaName, joinTableName)).append(" WHERE ").append(joinColumnName)
            .append("='").append(parentId).append("'");

    Session s = getSession();/* w  w  w . j av  a2s .c  o  m*/

    SQLQuery query = s.createSQLQuery(sqlQuery.toString());

    List<E> foreignKeys = new ArrayList<E>();

    foreignKeys = query.list();

    return foreignKeys;
}

From source file:com.impetus.client.rdbms.HibernateClient.java

License:Apache License

@Override
public Object[] findIdsByColumn(String schemaName, String tableName, String pKeyName, String columnName,
        Object columnValue, Class entityClazz) {
    String childIdStr = (String) columnValue;
    StringBuffer sqlQuery = new StringBuffer();
    sqlQuery.append("SELECT ").append(pKeyName).append(" FROM ").append(getFromClause(schemaName, tableName))
            .append(" WHERE ").append(columnName).append("='").append(childIdStr).append("'");

    Session s = getSession();//from  w  w w  .j a  v  a  2  s  .c  om

    SQLQuery query = s.createSQLQuery(sqlQuery.toString());

    List<Object> primaryKeys = new ArrayList<Object>();

    primaryKeys = query.list();

    if (primaryKeys != null && !primaryKeys.isEmpty()) {
        return primaryKeys.toArray(new Object[0]);
    }
    return null;
}

From source file:com.impetus.client.rdbms.HibernateClient.java

License:Apache License

/**
 * Find.//from  ww w.jav  a 2 s .  co  m
 * 
 * @param nativeQuery
 *            the native fquery
 * @param relations
 *            the relations
 * @param m
 *            the m
 * @return the list
 */
public List find(String nativeQuery, List<String> relations, EntityMetadata m) {
    List entities = new ArrayList();

    s = getStatelessSession();

    SQLQuery q = s.createSQLQuery(nativeQuery);
    q.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);

    List result = q.list();

    try {
        MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata()
                .getMetamodel(m.getPersistenceUnit());

        EntityType entityType = metaModel.entity(m.getEntityClazz());

        List<AbstractManagedType> subManagedType = ((AbstractManagedType) entityType).getSubManagedType();
        for (Object o : result) {
            Map<String, Object> relationValue = null;
            Object entity = null;
            EntityMetadata subEntityMetadata = null;
            if (!subManagedType.isEmpty()) {
                for (AbstractManagedType subEntity : subManagedType) {
                    String discColumn = subEntity.getDiscriminatorColumn();
                    String disColValue = subEntity.getDiscriminatorValue();
                    Object value = ((Map<String, Object>) o).get(discColumn);
                    if (value != null && value.toString().equals(disColValue)) {
                        subEntityMetadata = KunderaMetadataManager.getEntityMetadata(kunderaMetadata,
                                subEntity.getJavaType());
                        break;
                    }
                }
                entity = instantiateEntity(subEntityMetadata.getEntityClazz(), entity);
                relationValue = HibernateUtils.getTranslatedObject(kunderaMetadata, entity,
                        (Map<String, Object>) o, m);

            } else {
                entity = instantiateEntity(m.getEntityClazz(), entity);
                relationValue = HibernateUtils.getTranslatedObject(kunderaMetadata, entity,
                        (Map<String, Object>) o, m);
            }

            if (relationValue != null && !relationValue.isEmpty()) {
                entity = new EnhanceEntity(entity, PropertyAccessorHelper.getId(entity, m), relationValue);
            }
            entities.add(entity);
        }
        return entities;
    } catch (Exception e) {
        if (e.getMessage().equals("Can not be translated into entity.")) {
            return result;
        }
        throw new EntityReaderException(e);
    }
}

From source file:com.induscorp.prime.testing.ui.core.config.database.DatabaseQueryHandler.java

License:Open Source License

@SuppressWarnings("rawtypes")
public List executeSearchQuery(String query) {
    List foundRecords = null;/* w w  w  .  j a va2 s . c  o m*/
    Session hibSession = null;
    try {
        Thread.sleep(5000);

        hibSession = hibernateSessionFactory.openSession();

        SQLQuery sqlQuery = hibSession.createSQLQuery(query);
        foundRecords = sqlQuery.list();

    } catch (Exception ex) {
        Reporter.log("Error in executing query '" + query + "'.");
        Assert.fail("Error in executing query '" + query + "'.", ex);
    } finally {
        if (hibSession != null) {
            hibSession.close();
        }
    }

    return foundRecords;
}

From source file:com.induscorp.prime.testing.ui.core.config.database.DatabaseQueryHandler.java

License:Open Source License

@SuppressWarnings("unchecked")
public <T> List<T> executeSearchQuery(String query, Class<T> entityClass) {
    List<T> foundRecords = null;
    Session hibSession = null;//from  ww  w.  j ava 2s . c om
    try {
        Thread.sleep(5000);

        hibSession = hibernateSessionFactory.openSession();

        SQLQuery sqlQuery = hibSession.createSQLQuery(query);
        sqlQuery.addEntity(entityClass);
        foundRecords = sqlQuery.list();

    } catch (Exception ex) {
        Reporter.log("Error in executing query '" + query + "'.");
        Assert.fail("Error in executing query '" + query + "'.", ex);
    } finally {
        if (hibSession != null) {
            hibSession.close();
        }
    }

    return foundRecords;
}