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.sistcontable.dao.UsuarioDAO.java

public void searchUserByID(String id) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//from w  w w  . j  a  v a2 s  .c om
    List<Usuario> lista = new ArrayList();
    try {
        String query = "select * from usuario where id_usuario=:id_usuario";
        SQLQuery sqlQuery = session.createSQLQuery(query);
        sqlQuery.addEntity(Usuario.class);
        sqlQuery.setParameter("id_usuario", id);
        //lista=sqlQuery.list();   
        List result = sqlQuery.list();
        for (int i = 0; i < result.size(); i++) {
            System.out.println("id: ");
            Usuario usuario = (Usuario) result.get(i);

            System.out.println("id_usuario: " + usuario.getId_usuario());
            System.out.println("id_persona: " + usuario.getId_persona());
            System.out.println("nombre_usuario: " + usuario.getNombre_usuario());
            System.out.println("clave: " + usuario.getClave());
        }
    } catch (Exception e) {
        System.out.println("ERROR " + e);
        lista = null;
    } finally {
        session.close();
    }
}

From source file:com.sistcontable.dao.UsuarioDAO.java

public List<Usuario> searchAllUser() {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//from  w  ww.j ava 2  s .  c om
    List<Usuario> lista = new ArrayList();
    try {
        String query = "select * from usuario";
        SQLQuery sqlQuery = session.createSQLQuery(query);
        sqlQuery.addEntity(Usuario.class);
        lista = sqlQuery.list();
    } catch (Exception e) {
        System.out.println("ERROR " + e);
        lista = null;
    } finally {
        session.close();
    }
    return lista;
}

From source file:com.sistcontable.dao.UsuarioDAO.java

public Persona[] listaPersonas() {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*  ww  w .j a v a  2  s. c o  m*/
    Persona[] apersona = null;
    try {
        String query = "select id_persona,primer_nombre,primer_apellido from persona";
        SQLQuery sqlQuery = session.createSQLQuery(query);
        int i = 0;
        int longLista = sqlQuery.list().size();
        apersona = new Persona[longLista];
        for (i = 0; i < longLista; i++) {
            apersona[i] = new Persona();
        }
        System.out.println("longitud lista " + longLista);
        i = 0;
        List<Object[]> rows = sqlQuery.list();
        for (Object[] row : rows) {
            apersona[i].setIdPersona(BigDecimal.valueOf(Double.parseDouble(row[0].toString())));
            apersona[i].setPrimerNombre(row[1].toString());
            apersona[i].setPrimerApellido(row[2].toString());
            i++;
        }
    } catch (Exception e) {
        System.out.println("ERROR " + e);
    } finally {
        session.close();
    }
    return apersona;
}

From source file:com.smallfe.webapplicationtemplate.dao.ApplicationDaoImpl.java

@Override
public void testConnection() {
    String sql = "SELECT 1 FROM DUAL";
    SQLQuery query = this.getSession().createSQLQuery(sql);
    query.list();
}

From source file:com.software.team.BigStore.Controllers.QuestionController.java

public int getLastQuestionID() {
    SQLQuery query = session
            .createSQLQuery("SELECT `questionId` FROM `question` ORDER BY `questionId` DESC LIMIT 1");
    List<Integer> ids = query.list();
    if (ids.isEmpty()) {
        return 0;
    } else {//from  w ww  .  j  ava  2s .  c o m
        int q = ids.get(ids.size() - 1);
        System.out.println("question id = " + q);
        return q;
    }
}

From source file:com.software.team.BigStore.Controllers.QuestionController.java

private int getLastAnswerID() {
    SQLQuery query = session
            .createSQLQuery("SELECT `answer_id` FROM `answer` ORDER BY `answer_id` DESC LIMIT 1");
    List<Integer> ids = query.list();
    if (ids.isEmpty()) {
        return 0;
    } else {/*w  ww  . j  a va 2 s  .  com*/
        int a = ids.get(ids.size() - 1);
        System.out.println("answer id = " + a);
        return a;
    }
}

From source file:com.syntelinc.BOK.ATM.menupkg.Hibernate.java

public BigDecimal getBalance(int accountID, String accountType) {
    SQLQuery q;
    com.syntelinc.BOK.ATM.transactionpkg.Transaction transac;
    if (accountType.equals("checking")) {
        transac = new CheckingTransaction();
        q = session.createSQLQuery("select balance from checkingtrans where acctid=?");
    } else {/*  w ww.j a v a 2  s  .  c o  m*/
        transac = new SavingsTransaction();
        q = session.createSQLQuery("select balance from savingtrans where acctid=?");
    }
    transac.setAcctid(accountID);

    q.setInteger(0, transac.getAcctid());
    List ld = q.list();
    BigDecimal bal;
    if (ld.isEmpty())
        bal = new BigDecimal(0);
    else
        bal = (BigDecimal) ld.get(ld.size() - 1);
    return bal;
}

From source file:com.syntelinc.BOK.ATM.transactionpkg.HibernateTransaction.java

private void AccountTrans(Transaction tran) throws IllegalArgumentException {
    Configuration cfg = new Configuration().configure();
    SessionFactory sf = cfg.buildSessionFactory();
    Session s = sf.openSession();//  w w  w  . ja  va  2  s.  co  m
    org.hibernate.Transaction t = s.beginTransaction();

    SQLQuery q;
    if (sessionMap.get("accounttype").equals("checking")) {
        tran = new CheckingTransaction();
        q = s.createSQLQuery("select balance from checkingtrans where acctid=?");
    } else {
        tran = new SavingsTransaction();
        q = s.createSQLQuery("select balance from savingtrans where acctid=?");
    }

    tran.setAcctid(Integer.parseInt((String) sessionMap.get("accountid")));

    q.setInteger(0, tran.getAcctid());
    List ld = q.list();
    BigDecimal bal;
    if (ld.isEmpty())
        bal = new BigDecimal(0);
    else
        bal = (BigDecimal) ld.get(ld.size() - 1);

    tran.setTime(new Date());
    tran.setLocation("1234 North Vegas Street");
    DecimalFormat df = new DecimalFormat("#.##");
    df.setRoundingMode(RoundingMode.CEILING);
    tran.setDebitamt(Double.parseDouble((String) sessionMap.get("depositamt")));
    tran.setDebitamt(Double.parseDouble(df.format(tran.getDebitamt())));
    tran.setCreditamt(Double.parseDouble((String) sessionMap.get("withdrawamt")));
    tran.setCreditamt(Double.parseDouble(df.format(tran.getCreditamt())));
    switch ((String) sessionMap.get("type")) {
    case "cash":
        tran.setType(1);
        tran.setBalance(bal.doubleValue() + tran.getDebitamt() - tran.getCreditamt());
        tran.setBalance(Double.parseDouble(df.format(tran.getBalance())));
        break;
    case "check":
        tran.setType(2);
        tran.setBalance(bal.doubleValue());
        tran.setBalance(Double.parseDouble(df.format(tran.getBalance())));
        break;
    case "default":
        throw new IllegalArgumentException();
    }
    sessionMap.put("balance", tran.getBalance());

    s.save(tran);
    t.commit();

    s.close();
    sf.close();
}

From source file:com.syntelinc.BOK.ATM.withdrawpkg.CheckDailyLimit.java

public double getCurrentTotal(int accountid) {
    Configuration cfg = new Configuration().configure();
    SessionFactory sf = cfg.buildSessionFactory();
    Session s = sf.openSession();//from  ww w . j a  v a  2  s  .c om
    org.hibernate.Transaction t = s.beginTransaction();
    SQLQuery q;
    q = s.createSQLQuery("select creditamt from checkingtrans where acctid=? AND TIME < ? AND TIME > ?");
    q.setInteger(0, accountid);
    q.setTimestamp(1, getDayEnd());
    q.setTimestamp(2, getDayStart());
    List<BigDecimal> li = q.list();
    double totalCredit = 0;
    if (li.isEmpty())
        totalCredit = 0;
    else if (li.get(0) == null)
        totalCredit = 0;
    else {
        for (BigDecimal cred : li) {
            totalCredit += cred.doubleValue();
        }
    }

    System.out.println(totalCredit);

    if (!li.isEmpty())
        return totalCredit;
    else
        return 0;
}

From source file:com.sysware.customize.hd.investment.baseData.vendor.VendorDaoImpl.java

@SuppressWarnings("unchecked")
public List<Vendor> findByMaterialIds(String materialCatalogId, int begin, int max, String type) {
    if (materialCatalogId != null && materialCatalogId.length() > 0) {
        StringBuilder sqlStr = new StringBuilder(
                "select distinct v.VENDORID,v.VENDORNAME,v.VENDORCODE,v.ACCOUNTID,v.ADDRESS,"
                        + "v.BANK,v.BUSINESSSCOPE,v.INITIALEVALUATIONDATE,v.VENDORLEVEL,v.PHONE,"
                        + "v.REVIEWDATE,v.TAXID,v.REPOSAL,v.PROPERTY," +
                        //               "m.MATERIALITEMNAME," +
                        "v.simplename,v.type,v.sector ");
        sqlStr.append(" from t_vendor v, t_vendor_material vm, t_material m ");
        sqlStr.append(" where v.vendorid=vm.vendorid and vm.materialid=m.materialid and ");
        if (type.equals("0")) {
            sqlStr.append(" m.PARENTID in (");
            sqlStr.append(" select mc.materialcatalogid from t_materialcatalog mc ");
            sqlStr.append(" start with mc.materialcatalogid='" + materialCatalogId + "'");
            sqlStr.append(" connect by prior mc.materialcatalogid = mc.parentid) ");
        } else {/*from   ww w  .j  a va2 s . co m*/
            sqlStr.append(" vm.materialid = '" + materialCatalogId + "'");//materialCatalogId ?ID  
        }
        sqlStr.append(" order by v.vendorname ");
        //         System.out.println("?"+sqlStr.toString());
        List<Vendor> results = new ArrayList<Vendor>();
        //         List<Object[]> temps = this.executeNativeQuery(sqlStr.toString(),
        //               null, begin, max);
        //         for (Object[] objects : temps) {
        //            Vendor vendor = new Vendor();
        //            vendor.setVendorID((String) objects[0]);
        //            vendor.setVendorName(objects[1]==null?"":(String) objects[1]);
        //            vendor.setVendorCode(objects[2]==null?"":(String) objects[2]);
        //            vendor.setAccountID(objects[3]==null?"":(String) objects[3]);
        //            vendor.setAddress(objects[4]==null?"":(String) objects[4]);
        //            vendor.setBank(objects[5]==null?"":(String) objects[5]);
        //            vendor.setBusinessScope(objects[6]==null?"":(String) objects[6]);
        //            vendor.setInitialEvaluationDate(objects[7]==null?null:(Date) objects[7]);
        //            vendor.setVendorLevel(objects[8]==null?"":(String) objects[8]);
        //            vendor.setPhone(objects[9]==null?"":(String) objects[9]);
        ////            vendor.setReviewResult(objects[10]==null?"":(String) objects[10]);
        //            vendor.setReviewDate(objects[10]==null?null:(Date) objects[10]);
        //            vendor.setTaxID(objects[11]==null?"":(String) objects[11]);
        //            vendor.setReposal(objects[12]==null?"":(String) objects[12]);
        //            vendor.setProperty(objects[13]==null?"":(String) objects[13]);
        //             vendor.setMaterialItemName(objects[14]==null?"":(String) objects[14]);
        //             vendor.setSimpleName(objects[15]==null?"":(String) objects[15]);
        //             vendor.setType(objects[16]==null?"":(String) objects[16]);
        //             vendor.setSector(objects[17]==null?"":(String) objects[17]);
        //            results.add(vendor);
        //         }
        SQLQuery query = dao.getHibernateSession().createSQLQuery(sqlStr.toString());
        setFindByMaterialIds(query);
        query = (SQLQuery) query.setResultTransformer(Transformers.aliasToBean(Vendor.class));
        results = query.list();
        return results;
    } else {
        return new ArrayList<Vendor>();
    }

}