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.ephesoft.dcma.fuzzydb.FuzzyLuceneEngine.java

License:Open Source License

/**
 * This method fetches the data for a row returned by lucene search.
 * @param rowId long/*from  w ww .j ava  2  s . c  om*/
 * @param tableName {@link String}
 * @param dbConnectionURL {@link String}
 * @param dbName {@link String}
 * @param dbDriver {@link String}
 * @param dbUserName {@link String}
 * @param dbPassword {@link String}
 * @param eachConfig {@link com.ephesoft.dcma.batch.dao.impl.BatchPluginPropertyContainer.BatchDynamicPluginConfiguration}
 * @return {@link List<Object[]>}
 * @throws DCMAApplicationException if any exception occurs.
 */
@SuppressWarnings("unchecked")
public List<Object[]> fetchDataForRow(long rowId, String tableName, String dbConnectionURL, String dbName,
        String dbDriver, String dbUserName, String dbPassword, BatchDynamicPluginConfiguration eachConfig)
        throws DCMAApplicationException {
    Set<BatchDynamicPluginConfiguration> allColumnNames = null;
    List<Object[]> dataList = null;
    if (eachConfig != null) {
        allColumnNames = eachConfig.getChildren();
        if (allColumnNames != null && !allColumnNames.isEmpty()) {
            DynamicHibernateDao dynamicHibernateDao = null;
            try {
                dynamicHibernateDao = new DynamicHibernateDao(dbUserName, dbPassword, dbDriver,
                        dbConnectionURL);
                StringBuffer dbQuery = new StringBuffer("select ");
                int count = 0;
                String retrunFieldName = "id";
                if (allColumnNames != null && !allColumnNames.isEmpty()) {
                    for (BatchDynamicPluginConfiguration eachColumn : allColumnNames) {
                        count++;
                        appendColumnNameByDatabase(dbConnectionURL, allColumnNames, dbQuery, count, eachColumn);
                        if (eachColumn.getKey().equalsIgnoreCase(rowID)) {
                            if (dbConnectionURL.contains(FuzzyDBSearchConstants.MYSQL_DATABASE)) {
                                retrunFieldName = FuzzyDBSearchConstants.SINGLE_QUOTES + eachColumn.getValue()
                                        + FuzzyDBSearchConstants.SINGLE_QUOTES;
                            } else {
                                retrunFieldName = FuzzyDBSearchConstants.DOUBLE_QUOTES + eachColumn.getValue()
                                        + FuzzyDBSearchConstants.DOUBLE_QUOTES;
                            }
                        }
                    }
                } else {
                    LOGGER.info(FuzzyDBSearchConstants.COLUMN_NAME_ERROR_MSG);
                    throw new DCMAApplicationException(FuzzyDBSearchConstants.COLUMN_NAME_ERROR_MSG);
                }
                dbQuery.append(" from ").append(tableName).append(" where ").append(retrunFieldName)
                        .append(FuzzyDBSearchConstants.EQUALS).append(rowId);
                SQLQuery query = dynamicHibernateDao.createQuery(dbQuery.toString());
                dataList = query.list();
            } finally {
                if (dynamicHibernateDao != null) {
                    dynamicHibernateDao.closeSession();
                }
            }

        }
    }
    return dataList;
}

From source file:com.ephesoft.dcma.fuzzydb.FuzzyLuceneEngine.java

License:Open Source License

/**
 * This method fetches the data for a row returned by lucene search.
 * @param rowId long/*from  w w  w .j av a  2s.c  o m*/
 * @param confidenceScore {@link String}
 * @param tableName {@link String}
 * @param dbConnectionURL {@link String}
 * @param dbName {@link String}
 * @param dbDriver {@link String}
 * @param dbUserName {@link String}
 * @param dbPassword {@link String}
 * @param eachConfig {@link com.ephesoft.dcma.batch.dao.impl.BatchPluginPropertyContainer.BatchDynamicPluginConfiguration}
 * @param isHeaderAdded boolean
 * @return {@link List<List<String>>}
 * @throws DCMAApplicationException if any exception occurs.
 */
@SuppressWarnings("unchecked")
public List<List<String>> fetchFuzzySearchResult(long rowId, String confidenceScore, String tableName,
        String dbConnectionURL, String dbName, String dbDriver, String dbUserName, String dbPassword,
        BatchDynamicPluginConfiguration eachConfig, boolean isHeaderAdded) throws DCMAApplicationException {
    Set<BatchDynamicPluginConfiguration> allColumnNames = null;
    List<List<String>> extractedData = null;
    if (eachConfig != null) {
        extractedData = new ArrayList<List<String>>();
        allColumnNames = eachConfig.getChildren();
        if (allColumnNames != null && !allColumnNames.isEmpty()) {
            DynamicHibernateDao dynamicHibernateDao = null;
            try {
                dynamicHibernateDao = new DynamicHibernateDao(dbUserName, dbPassword, dbDriver,
                        dbConnectionURL);
                StringBuffer dbQuery = new StringBuffer("select ");
                int count = 0;
                String retrunFieldName = "id";

                if (allColumnNames != null && !allColumnNames.isEmpty()) {
                    List<String> list = new ArrayList<String>();
                    for (BatchDynamicPluginConfiguration eachColumn : allColumnNames) {
                        list.add(eachColumn.getDescription());
                        count++;
                        appendColumnNameByDatabase(dbConnectionURL, allColumnNames, dbQuery, count, eachColumn);
                        if (eachColumn.getKey().equalsIgnoreCase(rowID)) {
                            if (dbConnectionURL.contains(FuzzyDBSearchConstants.MYSQL_DATABASE)) {
                                retrunFieldName = FuzzyDBSearchConstants.SINGLE_QUOTES + eachColumn.getValue()
                                        + FuzzyDBSearchConstants.SINGLE_QUOTES;
                            } else {
                                retrunFieldName = FuzzyDBSearchConstants.DOUBLE_QUOTES + eachColumn.getValue()
                                        + FuzzyDBSearchConstants.DOUBLE_QUOTES;
                            }
                        }
                    }
                    if (isHeaderAdded) {
                        list.add("Confidence Score");
                        extractedData.add(list);
                    }
                } else {
                    LOGGER.info(FuzzyDBSearchConstants.COLUMN_NAME_ERROR_MSG);
                    throw new DCMAApplicationException(FuzzyDBSearchConstants.COLUMN_NAME_ERROR_MSG);
                }
                dbQuery.append(new StringBuffer(" from ")).append(tableName).append(new StringBuffer(" where "))
                        .append(retrunFieldName).append(new StringBuffer(FuzzyDBSearchConstants.EQUALS))
                        .append(rowId);
                SQLQuery query = dynamicHibernateDao.createQuery(dbQuery.toString());
                List<Object[]> dataList = query.list();

                if (null != dataList) {
                    for (Object[] obj : dataList) {
                        List<String> list = new ArrayList<String>();
                        for (Object object : obj) {
                            if (object == null) {
                                object = new StringBuffer(FuzzyDBSearchConstants.SPACE_DELIMITER);
                            }
                            list.add(object.toString());
                        }
                        list.add(confidenceScore);
                        extractedData.add(list);
                    }
                }
            } finally {
                if (dynamicHibernateDao != null) {
                    dynamicHibernateDao.closeSession();
                }
            }
        }
    }
    return extractedData;
}

From source file:com.ephesoft.dcma.fuzzydb.FuzzyLuceneEngine.java

License:Open Source License

/**
 * This method finds all the records that needs to be indexed for all the tables configured for document types.
 * @param tableName {@link String}/* w  w w . ja v  a 2 s. c om*/
 * @param dbConnectionURL {@link String}
 * @param dbName {@link String}
 * @param dbDriver {@link String}
 * @param dbUserName {@link String}
 * @param dbPassword {@link String}
 * @param dateFormat {@link String}
 * @param eachConfig {@link com.ephesoft.dcma.batch.dao.impl.BatchPluginPropertyContainer.BatchDynamicPluginConfiguration}
 * @return {@link List<String>}
 * @throws DCMAApplicationException if any exception occurs.
 */
@SuppressWarnings("unchecked")
public List<String> fetchAllRecordsToBeIndexed(String tableName, String dbConnectionURL, String dbName,
        String dbDriver, String dbUserName, String dbPassword, String dateFormat,
        BatchDynamicPluginConfiguration eachConfig) throws DCMAApplicationException {
    List<String> returnList = null;
    Set<BatchDynamicPluginConfiguration> allColumnNames = null;
    if (eachConfig != null) {
        allColumnNames = eachConfig.getChildren();
        if (allColumnNames != null && !allColumnNames.isEmpty()) {
            DynamicHibernateDao dynamicHibernateDao = null;
            try {
                dynamicHibernateDao = new DynamicHibernateDao(dbUserName, dbPassword, dbDriver,
                        dbConnectionURL);
                List<ColumnDefinition> colNames = null;
                try {
                    colNames = dynamicHibernateDao.getAllColumnsForTable(tableName);
                } catch (SQLException e) {
                    LOGGER.info("Could not find Column names from table : " + tableName);
                }
                StringBuffer dbQuery = new StringBuffer("select ");
                int count = 0;
                List<Integer> dateColIndex = new ArrayList<Integer>();
                for (BatchDynamicPluginConfiguration eachColumn : allColumnNames) {
                    count++;
                    appendColumnNameByDatabase(dbConnectionURL, allColumnNames, dbQuery, count, eachColumn);
                    String colDataType = getColumnDataType(eachColumn.getValue(), colNames);
                    if (colDataType != null
                            && (colDataType.equalsIgnoreCase(FuzzyDBSearchConstants.TIMESTAMP_NAME)
                                    || colDataType.equalsIgnoreCase(FuzzyDBSearchConstants.DATE_NAME))) {
                        dateColIndex.add(count);
                    }
                }
                dbQuery.append(new StringBuffer(" from ")).append(tableName);
                SQLQuery query = dynamicHibernateDao.createQuery(dbQuery.toString());
                List<Object[]> dataList = query.list();

                StringBuffer dbRow = new StringBuffer(FuzzyDBSearchConstants.EMPTY_STRING);
                int indexCount = 0;
                if (dataList != null && !dataList.isEmpty()) {
                    returnList = new ArrayList<String>();
                    if (dataList.size() > 1) {
                        LOGGER.info("More than One records found. So picking first record.");
                    }
                    for (Object[] eachRecord : dataList) {
                        indexCount = 0;
                        dbRow = new StringBuffer(FuzzyDBSearchConstants.EMPTY_STRING);
                        for (Object eachElement : eachRecord) {
                            indexCount++;
                            if (eachElement != null) {
                                if (dateColIndex.contains(indexCount)) {
                                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat,
                                            Locale.getDefault());
                                    eachElement = simpleDateFormat.format(eachElement);
                                }
                                dbRow.append(eachElement).append(";;;");
                            } else {
                                dbRow.append(" ;;;");
                            }
                        }
                        returnList.add(dbRow.toString());
                    }
                } else {
                    LOGGER.info("Unable to fetch data for query : " + dbQuery);
                }
            } finally {
                if (dynamicHibernateDao != null) {
                    dynamicHibernateDao.closeSession();
                }
            }
        } else {
            LOGGER.info("No column names configured for table name : " + eachConfig.getValue());
        }
    }
    return returnList;
}

From source file:com.epms.dao.UserDaoImpl.java

@Override
public Boolean authenticateUser(String username, String password) {
    String queryUser = "SELECT * from user where UserName = '" + username + "' AND Password = '" + password
            + "'";
    SQLQuery sqlQuery = getHibernateTemplate().getSessionFactory().openSession().createSQLQuery(queryUser);
    List result = sqlQuery.list();
    return !result.isEmpty();
}

From source file:com.esp.dao.GenericDAO.java

@Override
public BigDecimal Count(String query) {

    BigDecimal t;/*from  w  w  w . j  av a  2s . c  om*/
    SQLQuery qry = sessionFactory.getCurrentSession().createSQLQuery(query);
    t = (BigDecimal) qry.list().get(0);
    return t;
}

From source file:com.ett.drv.biz.DrvQueryHelperImp.java

public List<ComboxObject> getDict(String xtlb, String type) {
    List<ComboxObject> listTmp = new ArrayList<ComboxObject>();
    SessionFactory sessionFactory = this.getBaseDaoDrv().getHibernateSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();

    String queryStr = MessageFormat.format(DictSql, xtlb, type);
    logger.debug("?" + queryStr.toString());
    SQLQuery query = session.createSQLQuery(queryStr.toString());
    query.addScalar("value", new org.hibernate.type.StringType());
    query.addScalar("text", new org.hibernate.type.StringType());

    query.setResultTransformer(Transformers.aliasToBean(ComboxObject.class));
    try {/*www  . jav  a  2s.c  o  m*/
        tx.begin();
        //query.uniqueResult()
        listTmp = query.list();

        if (logger.isInfoEnabled()) {
            logger.info("==" + listTmp.size());
        }
        if (listTmp.size() == 0) {
            throw new ObjectDontExistException("?");
        }
        session.flush();
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        logger.info(e);
        if (tx != null) {
            tx.rollback();
        }
        throw new ObjectDontExistException(e);
    } finally {
        session.close();
    }
    return listTmp;
}

From source file:com.ett.drv.biz.DrvQueryHelperImp.java

public DrvUser getDrvUser(String idcard, String glbm) {
    //System.out.println("Dao");
    logger.debug("DAO" + this.getBaseDaoDrv());
    logger.debug("??" + this.user);
    if (this.user != null) {

        return user;
    }/*  w w w .  j a v  a  2s .c o  m*/
    logger.debug("???" + idcard + "??" + glbm
            + "??");
    List lists = new ArrayList();
    SessionFactory sessionFactory = this.getBaseDaoDrv().getHibernateSessionFactory();
    Session session = sessionFactory.openSession();
    logger.debug("baseDaoDrv?");
    Transaction tx = session.beginTransaction();
    logger.debug("Sessionsql?");
    String queryStr = MessageFormat.format(getDrvUser, idcard, glbm);
    logger.debug("??" + queryStr.toString());
    SQLQuery query = session.createSQLQuery(queryStr.toString());
    query.addScalar("lsh", new org.hibernate.type.StringType());
    query.addScalar("sfzmhm", new org.hibernate.type.StringType());
    query.addScalar("xb", new org.hibernate.type.IntegerType());
    query.addScalar("xm", new org.hibernate.type.StringType());
    query.addScalar("csrq", new org.hibernate.type.DateType());
    query.addScalar("lxzsxxdz", new org.hibernate.type.StringType());
    query.addScalar("lxdh", new org.hibernate.type.StringType());
    query.addScalar("sjhm", new org.hibernate.type.StringType());

    query.setResultTransformer(Transformers.aliasToBean(DrvUser.class));
    try {
        tx.begin();
        //query.uniqueResult()
        lists = query.list();

        if (logger.isInfoEnabled()) {
            logger.info("??==" + lists.size());
        }
        if (lists.size() == 0) {
            return null;
            //throw new ObjectDontExistException("??");
        }

        session.flush();
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();

        logger.info(e);
        if (tx != null) {
            tx.rollback();
        }

        throw new ObjectDontExistException(e);
    } finally {
        session.close();
    }
    this.user = (DrvUser) lists.get(0);
    return this.user;
}

From source file:com.ett.drv.biz.DrvQueryHelperImp.java

public DrvUser getUserWithExamCard(DrvUser user) {
    DrvUser tmpuser = user;//from w  ww.  j a v a2s. c  o  m
    List lists = new ArrayList();
    SessionFactory sessionFactory = this.getBaseDaoDrv().getHibernateSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();

    String queryStr = MessageFormat.format(getExamCard, tmpuser.getSfzmhm());
    logger.debug("??" + queryStr.toString());
    SQLQuery query = session.createSQLQuery(queryStr.toString());
    query.addScalar("dabh", new org.hibernate.type.StringType());
    query.addScalar("zkzmbh", new org.hibernate.type.StringType());
    query.addScalar("zkcx", new org.hibernate.type.StringType());
    query.addScalar("cclzrq", new org.hibernate.type.TimestampType());
    query.addScalar("fzrq", new org.hibernate.type.TimestampType());
    query.addScalar("jxdm", new org.hibernate.type.StringType());

    //GregorianCalendar
    query.addScalar("yxqz", new org.hibernate.type.TimestampType());
    query.addScalar("yxqs", new org.hibernate.type.TimestampType());

    query.setResultTransformer(Transformers.aliasToBean(DrvUser.class));
    try {
        tx.begin();
        //query.uniqueResult()
        lists = query.list();

        if (logger.isInfoEnabled()) {
            logger.info("getUserWithExamCard?==" + lists.size());
        }
        if (lists.size() == 0) {
            return null;
            //throw new ObjectDontExistException("???");
        }

        session.flush();
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();

        logger.info(e);
        if (tx != null) {
            tx.rollback();
        }

        throw new ObjectDontExistException(e);
    } finally {
        session.close();
    }
    DrvUser tmp = (DrvUser) lists.get(0);
    tmpuser.setDabh(tmp.getDabh());
    tmpuser.setZkzmbh(tmp.getZkzmbh());
    tmpuser.setYxqs(tmp.getYxqs());
    tmpuser.setYxqz(tmp.getYxqz());
    tmpuser.setZkcx(tmp.getZkcx());
    tmpuser.setCclzrq(tmp.getCclzrq());
    tmpuser.setFzrq(tmp.getFzrq());
    tmpuser.setJxdm(tmp.getJxdm());
    return tmpuser;
}

From source file:com.ett.drv.biz.DrvQueryHelperImp.java

public DrvUser getUserWithLicense(DrvUser user) {
    DrvUser tmpuser = user;/*from w w w . jav  a2 s  .c  om*/
    List lists = new ArrayList();
    SessionFactory sessionFactory = this.getBaseDaoDrv().getHibernateSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();

    String queryStr = MessageFormat.format(getLicense, tmpuser.getSfzmhm());
    logger.debug("getUserWithLicense?" + queryStr.toString());
    SQLQuery query = session.createSQLQuery(queryStr.toString());
    query.addScalar("zkcx", new org.hibernate.type.StringType());
    query.addScalar("dabh", new org.hibernate.type.StringType());
    query.addScalar("yxqz", new org.hibernate.type.TimestampType());
    query.addScalar("yxqs", new org.hibernate.type.TimestampType());
    query.addScalar("cclzrq", new org.hibernate.type.TimestampType());
    query.addScalar("syrq", new org.hibernate.type.TimestampType());
    query.addScalar("qfrq", new org.hibernate.type.TimestampType());
    query.addScalar("jxdm", new org.hibernate.type.StringType());

    query.setResultTransformer(Transformers.aliasToBean(DrvUser.class));
    try {
        tx.begin();
        //query.uniqueResult()
        lists = query.list();

        if (logger.isInfoEnabled()) {
            logger.info("getUserWithLicense==" + lists.size());
        }
        if (lists.size() == 0) {
            return null;
            //throw new ObjectDontExistException("???");
        }

        session.flush();
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();

        logger.info(e);
        if (tx != null) {
            tx.rollback();
        }

        throw new ObjectDontExistException(e);
    } finally {
        session.close();
    }
    DrvUser tmp = (DrvUser) lists.get(0);
    tmpuser.setDabh(tmp.getDabh());

    tmpuser.setYxqs(tmp.getYxqs());
    tmpuser.setYxqz(tmp.getYxqz());

    tmpuser.setCclzrq(tmp.getCclzrq());
    tmpuser.setQfrq(tmp.getQfrq());
    tmpuser.setSyrq(tmp.getSyrq());
    tmpuser.setJxdm(tmp.getJxdm());
    tmpuser.setZkcx(tmp.getZkcx());
    return tmpuser;
}

From source file:com.ett.drv.biz.DrvQueryHelperImp.java

public List<ComboxObject> getDict(String xtlb, String type, String glbm) {
    List<ComboxObject> listTmp = new ArrayList<ComboxObject>();
    SessionFactory sessionFactory = this.getBaseDaoDrv().getHibernateSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();

    String queryStr = MessageFormat.format(DictSqlWithGlbm, xtlb, type, glbm);
    logger.debug("DictSqlWithGlbm?" + queryStr.toString());
    SQLQuery query = session.createSQLQuery(queryStr.toString());
    query.addScalar("value", new org.hibernate.type.StringType());
    query.addScalar("text", new org.hibernate.type.StringType());

    query.setResultTransformer(Transformers.aliasToBean(ComboxObject.class));
    try {/*from   w ww. j a  v a  2 s  .c  om*/
        tx.begin();
        //query.uniqueResult()
        listTmp = query.list();

        if (logger.isInfoEnabled()) {
            logger.info("==" + listTmp.size());
        }
        if (listTmp.size() == 0) {
            return null;
            //throw new ObjectDontExistException("?");
        }
        session.flush();
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        logger.info(e);
        if (tx != null) {
            tx.rollback();
        }
        throw new ObjectDontExistException(e);
    } finally {
        session.close();
    }
    return listTmp;
}