Example usage for org.hibernate Query setParameter

List of usage examples for org.hibernate Query setParameter

Introduction

In this page you can find the example usage for org.hibernate Query setParameter.

Prototype

@SuppressWarnings("unchecked")
Query<R> setParameter(int position, Object val);

Source Link

Document

Bind a positional query parameter using its inferred Type.

Usage

From source file:benchmark.BenchmarkHnP.java

License:Apache License

/** Update a charge of the order items */
@SuppressWarnings("unchecked")
public void useUpdate() {

    long time1 = System.currentTimeMillis();
    Transaction tr = session.beginTransaction();

    String hql = "from HbmOrderItem where deleted = :deleted and order.deleted = :deleted";
    Query query = session.createQuery(hql);
    query.setParameter("deleted", false);
    List<HbmOrderItem> items = (List<HbmOrderItem>) query.list();

    int i = 0;/*from ww  w .  ja v  a2  s  .  c  o m*/
    for (HbmOrderItem item : items) {
        ++i;
        item.setCharge(item.getCharge().add(BigDecimal.ONE));
        session.update(item);
    }

    tr.commit();
    printTime("UPDATE " + i, time1, System.currentTimeMillis());
}

From source file:benchmark.BenchmarkHnP.java

License:Apache License

/** Create database and using DELETE */
@SuppressWarnings("unchecked")
public void useDelete() {

    long time1 = System.currentTimeMillis();
    Transaction tr = session.beginTransaction();

    String hql = "from HbmOrder";
    Query query = session.createQuery(hql);
    List<HbmOrder> orders = (List<HbmOrder>) query.list();

    for (HbmOrder order : orders) {

        hql = "delete from HbmOrderItem it where it.order = :order";
        query = session.createQuery(hql);
        query.setParameter("order", order);
        int rows = query.executeUpdate();

        session.delete(order);/* w  w w.  j  a va2  s  .  co  m*/
    }

    session.flush();

    hql = "delete from HbmUser";
    query = session.createQuery(hql);
    int rows = query.executeUpdate();

    tr.commit();
    printTime("DELETE", time1, System.currentTimeMillis());
}

From source file:benchmark.BenchmarkHnU.java

License:Apache License

/** Create database and using SELECT */
@SuppressWarnings("unchecked")
public void useSingleSelect() {

    long time1 = System.currentTimeMillis();
    Transaction tr = session.beginTransaction();

    String hql = "from PrfOrderItem where deleted = :deleted and order.deleted = :deleted";
    Query query = session.createQuery(hql);
    query.setParameter("deleted", false);
    List<PrfOrderItem> items = (List<PrfOrderItem>) query.list();

    int i = 0;//from  ww w.  j ava2s  . c o  m
    for (PrfOrderItem item : items) {
        ++i;
        Long id = item.getId();
        BigDecimal price = item.getPrice();
        if (false) {
            System.out.println(">>> Item.id: " + id + " " + price);
        }
    }

    tr.commit();
    printTime("SINGLE SELECT " + i, time1, System.currentTimeMillis());
}

From source file:benchmark.BenchmarkHnU.java

License:Apache License

/** Create database and using SELECT */
@SuppressWarnings("unchecked")
public void useEmptySelect() {

    long time1 = System.currentTimeMillis();
    Transaction tr = session.beginTransaction();

    for (int i = -ORDER_COUNT; i < 0; i++) {
        String hql = "from PrfOrder where id = :id and deleted = :deleted";
        Query query = session.createQuery(hql);
        query.setParameter("id", new Long(i));
        query.setParameter("deleted", true);
        List<PrfOrder> items = (List<PrfOrder>) query.list();
    }//ww w. j a v a 2  s  .  c o  m

    tr.commit();
    printTime("EMPTY SELECT " + ORDER_COUNT, time1, System.currentTimeMillis());
    ;

}

From source file:benchmark.BenchmarkHnU.java

License:Apache License

/** Create database and using SELECT */
@SuppressWarnings("unchecked")
public void useMultiSelect() {

    long time1 = System.currentTimeMillis();
    Transaction tr = session.beginTransaction();

    String hql = "from PrfOrder where deleted = :deleted";
    Query query = session.createQuery(hql);
    query.setParameter("deleted", false);
    List<PrfOrder> orders = (List<PrfOrder>) query.list();

    int i = 0;/*from w ww.  j a va  2s.  c  om*/
    for (PrfOrder order : orders) {
        String surename = order.getUser().getSurename();
        if (false) {
            System.out.println("Usr.surename: " + surename);
        }

        hql = "from PrfOrderItem where deleted = :deleted and order = :order";
        query = session.createQuery(hql);
        query.setParameter("deleted", false);
        query.setParameter("order", order);
        List<PrfOrderItem> items = (List<PrfOrderItem>) query.list();

        for (PrfOrderItem item : items) {
            ++i;
            BigDecimal price = item.getPrice();
            BigDecimal charge = item.getCharge();
            if (true) {
                String lang = item.getOrder().getLanguage();
                String name = item.getUser().getLastname();
                if (false) {
                    System.out.println(">>> Order.lang: " + lang + " User.lastname" + name);
                }
            }
        }
    }

    tr.commit();
    printTime("MULTI SELECT " + i, time1, System.currentTimeMillis());
}

From source file:benchmark.BenchmarkHnU.java

License:Apache License

/** Update a charge of the order items */
@SuppressWarnings("unchecked")
public void useUpdate() {

    long time1 = System.currentTimeMillis();
    Transaction tr = session.beginTransaction();

    String hql = "from PrfOrderItem where deleted = :deleted and order.deleted = :deleted";
    Query query = session.createQuery(hql);
    query.setParameter("deleted", false);
    List<PrfOrderItem> items = (List<PrfOrderItem>) query.list();

    int i = 0;//w  w w.  j a va2s. c om
    for (PrfOrderItem item : items) {
        ++i;
        item.setCharge(item.getCharge().add(BigDecimal.ONE));
        session.update(item);
    }

    tr.commit();
    printTime("UPDATE " + i, time1, System.currentTimeMillis());
}

From source file:benchmark.BenchmarkHnU.java

License:Apache License

/** Create database and using DELETE */
@SuppressWarnings("unchecked")
public void useDelete() {

    long time1 = System.currentTimeMillis();
    Transaction tr = session.beginTransaction();

    String hql = "from PrfOrder";
    Query query = session.createQuery(hql);
    List<PrfOrder> orders = (List<PrfOrder>) query.list();

    for (PrfOrder order : orders) {

        hql = "delete from PrfOrderItem it where it.order = :order";
        query = session.createQuery(hql);
        query.setParameter("order", order);
        int rows = query.executeUpdate();

        session.delete(order);//  ww  w.jav  a  2  s. c  o m
    }

    session.flush();

    hql = "delete from PrfUser";
    query = session.createQuery(hql);
    int rows = query.executeUpdate();

    tr.commit();
    printTime("DELETE", time1, System.currentTimeMillis());
}

From source file:bgroup.oracle.dao.ContractDaoImpl.java

@Override
public Contract getContract(int PATIENT_ID) {
    logger.debug("Start getDog");

    Session session = getSession();// www  . ja  va2s .  c o  m
    logger.debug("session:" + session.toString());
    Query query = session.createSQLQuery("SELECT  p.KeyID as keyId"
            + ",(SELECT text FROM lu WHERE keyid = 93114) AS \"doverennost\""
            + ",(SELECT text FROM lu WHERE keyid = 93114) AS \"doverennost2\""
            + ",(SELECT text FROM lu WHERE keyid = 93114) AS \"doverennost3\""
            + ",(SELECT text FROM lu WHERE keyid = 93114) AS \"doverennost4\""
            + ",(SELECT text FROM lu WHERE keyid = 93116) AS \"license\""
            + ",(SELECT text FROM lu WHERE keyid = 93116) AS \"license2\""
            + ",(SELECT text FROM lu WHERE keyid = 93116) AS \"license3\""
            + ",(SELECT text FROM lu WHERE keyid = 93116) AS \"license4\""
            + ",fn_pat_name_by_id (p.KeyID) AS \"fio\"" + ",fn_pat_name_by_id (p.KeyID) AS \"fio2\""
            + ",fn_pat_name_by_id (p.KeyID) AS \"fio3\"" + ",fn_pat_name_by_id (p.KeyID) AS \"fio4\""
            + ",fn_pat_name_by_id (p.KeyID) AS \"fio5\"" + ",fn_pat_name_by_id (p.KeyID) AS \"fio6\""
            + ",p.num AS patnum" + ",p.num AS patnum2" + ",pkg_kladr.get_address(P.KeyID,1) AS \"region1\""
            + ",pkg_kladr.get_address(P.KeyID,1) AS \"address\""
            + ",pkg_kladr.get_address(P.KeyID,1) AS \"address2\""
            + ",pkg_kladr.get_address(P.KeyID,1) AS \"address3\""
            + ",pkg_kladr.get_address(P.KeyID,1) AS \"address4\"" + ",fn_pat_phone_by_id(p.keyid) AS phone"
            + ",fn_pat_phone_by_id(p.keyid) AS phone2" + ",decode (p.sex, 1, '.','.') AS sex_name"
            + ",fn_man_by_id (gsp_get_user_id()) AS registrator"
            + ",fn_man_by_id (gsp_get_user_id()) AS registrator2"
            + ",fn_man_by_id (gsp_get_user_id()) AS registrator3"
            + ",fn_man_by_id (gsp_get_user_id()) AS registrator4"
            + ",fn_man_by_id (gsp_get_user_id()) AS registrator5"
            + ",TO_CHAR (p.birthdate, 'dd-mm-yyyy') AS birth_date"
            + ", NVL ((SELECT c.text FROM company c,agr a WHERE a.keyid = p.agrid AND c.keyid = a.companyid),'') AS company"
            + ", NVL ((SELECT MAX('? ' || l.ser || ' N ' || l.code) FROM police l,agr a WHERE a.keyid = p.agrid "
            + "AND l.patientid = p.keyid AND l.agrid = a.keyid AND NVL (l.status,0) <> 1),'') AS police"
            + ", '' AS privils\n" + ",P.CELLULAR\n" + "FROM PATIENT p WHERE p.keyid = :PATIENT_ID")
            .setResultTransformer(Transformers.aliasToBean(Contract.class))
    //.addEntity(Contract.class)
    ;
    query.setParameter("PATIENT_ID", PATIENT_ID);
    //List<Object[]> result = null;
    List<Contract> result = null;
    try {
        result = query.list();
    } catch (Exception e) {
        logger.error(e.toString());
    }
    Contract contract = null;
    if (result != null && result.size() == 1) {
        logger.info("- ");
        return result.get(0);
        //for (Object[] row : result) {
        //  for (Contract row : result) {
        //contract = new Contract();
        //    try {
        //   contract = (Contract) row[0];
        // for (Object obj : row) {
        //   System.out.print(obj + "::");
        // }

        /*         int i = 0;
                
            contract.setKeyId(Integer.parseInt(row[i++].toString()));
            contract.setDoverennost(getRowValue(row[i++]));
            contract.setDoverennost2(getRowValue(row[i++]));
            contract.setDoverennost3(getRowValue(row[i++]));
            contract.setDoverennost4(getRowValue(row[i++]));
            contract.setLicense(getRowValue(row[i++]));
            contract.setLicense2(getRowValue(row[i++]));
            contract.setLicense3(getRowValue(row[i++]));
            contract.setLicense4(getRowValue(row[i++]));
            contract.setFio(getRowValue(row[i++]));
            contract.setFio2(getRowValue(row[i++]));
            contract.setFio3(getRowValue(row[i++]));
            contract.setFio4(getRowValue(row[i++]));
            contract.setFio5(getRowValue(row[i++]));
            contract.setFio6(getRowValue(row[i++]));
            contract.setPatnum(getRowValue(row[i++]));
            contract.setPatnum2(getRowValue(row[i++]));
            contract.setRegion1(getRowValue(row[i++]));
            contract.setAddress(getRowValue(row[i++]));
            contract.setAddress2(getRowValue(row[i++]));
            contract.setAddress3(getRowValue(row[i++]));
            contract.setAddress4(getRowValue(row[i++]));
            contract.setPhone(getRowValue(row[i++]));
            contract.setPhone2(getRowValue(row[i++]));
            contract.setSEX_NAME(getRowValue(row[i++]));
            contract.setRegistrator(getRowValue(row[i++]));
            contract.setREGISTRATOR2(getRowValue(row[i++]));
            contract.setREGISTRATOR3(getRowValue(row[i++]));
            contract.setREGISTRATOR4(getRowValue(row[i++]));
            contract.setREGISTRATOR5(getRowValue(row[i++]));
            contract.setBIRTH_DATE(getRowValue(row[i++]));
            contract.setCOMPANY(getRowValue(row[i++]));
            contract.setPOLICE(getRowValue(row[i++]));
            contract.setPRIVILS(getRowValue(row[i++]));
            contract.setCELLULAR(getRowValue(row[i++]));
            //System.out.println("\n");
            */
        //    } catch (Exception e) {
        //      logger.error(e.toString());
        //}

        // }
    } else {
        logger.info("  ");
    }
    return null;
}

From source file:bgroup.oracle.dao.HelpFioDaoImpl.java

@Override
public HelpFio getHelpFio(int PATIENT_ID) {
    Session session = getSession();/*from www  .j  a v a 2  s.  c  o m*/
    Query query = session.createSQLQuery("SELECT \n" + "       fn_pat_name_by_id(keyid) AS fio, \n"
            + "       fn_pat_name_by_id(keyid) AS fio2, \n" + "       fn_pat_name_by_id(keyid) AS fio3, \n"
            + "       fn_pat_name_by_id(keyid) AS fio5, \n"
            + "(SELECT text FROM lu WHERE keyid = 93116) AS \"license\",\n"
            + "       fn_pat_name_by_id(keyid) AS fio4,\n"
            + "\t/*fn_man_by_id (gsp_get_user_id()) AS registrator,*/\n" + "\tSex as pol,\n"
            + "\tNVL(snils, NULL ) AS snils,\n" + "\tNVL(snils, NULL ) AS snils1,\n"
            + "\ts_spravka_num.nextval AS N,\n" + "\ts_spravka_num.nextval AS N2,\n" + "       num AS PATNUM,\n"
            + "       TO_CHAR(SYSDATE,'dd.mm.yyyy') AS GIVEDATE,\n"
            + "       TO_CHAR(SYSDATE,'dd.mm.yyyy') AS GIVEDATE2\n"
            + "FROM patient WHERE keyid = :PATIENT_ID\n")
            .setResultTransformer(Transformers.aliasToBean(HelpFio.class));
    query.setParameter("PATIENT_ID", PATIENT_ID);
    //List<Object[]> result = null;
    List<HelpFio> result = null;
    try {
        result = query.list();
    } catch (Exception e) {
        logger.error(e.toString());
    }
    Contract contract = null;
    if (result != null && result.size() == 1) {
        return result.get(0);
    } else {
        logger.info("  ");
    }
    return null;
}

From source file:biomart.DAO.AdminDAO.java

public String removeProductDetails(String productName) {
    Session session = Util.getSessionFactory().openSession();
    Query query = session.createQuery("delete from ProductBean where Product_name=:pn");
    query.setParameter("pn", productName);
    session.close();//from   w  w  w .  j a va2 s .c  o  m
    return "success";
}