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:com.neu.web.phmis.dao.AdminDao.java

public String authenticate(String username, String password) {

    String flag = "0";

    try {//from w w  w  . ja v  a  2  s  .com

        session = HibernateUtil.getSessionFactory().openSession();
        SQLQuery query = session
                .createSQLQuery("SELECT COUNT(*) FROM admin WHERE username = :user AND password = :pass");
        query.setString("user", username);
        query.setString("pass", password);
        List<BigInteger> list = query.list();
        int count = list.get(0).intValue();
        if (count == 1) {
            flag = "1";
        }

    } catch (HibernateException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        session.close();
        return flag;
    }

}

From source file:com.neu.web.phmis.dao.CommonDao.java

public UserBean activateAccount(UserBean userBean, String key) {

    try {/*from www.j ava2  s.  c o  m*/

        session = HibernateUtil.getSessionFactory().openSession();

        session.beginTransaction();
        SQLQuery query = session.createSQLQuery("CALL activateAccount (:id, :password, :key, :ipAddr)");
        query.setInteger("id", userBean.getId());
        query.setString("password", userBean.getPassword());
        query.setString("key", key);
        query.setString("ipAddr", userBean.getLastAccessedFrom());

        List<UserBean> list = query.addEntity(UserBean.class).list();
        if (!list.isEmpty()) {
            userBean = list.get(0);
            Hibernate.initialize(userBean.getEnterpriseBean());
            Hibernate.initialize(userBean.getRoleBean());
        }

        session.getTransaction().commit();

    } catch (HibernateException e) {
        session.getTransaction().rollback();
        userBean = null;
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        session.close();
        return userBean;
    }

}

From source file:com.neu.web.phmis.dao.CommonDao.java

public UserBean authenticate(UserBean userBean, String ipAddr) {

    try {//from  ww w . ja va 2  s  .  c o m

        session = HibernateUtil.getSessionFactory().openSession();

        session.beginTransaction();
        SQLQuery query = session.createSQLQuery("CALL authenticateUser (:username, :password, :ipAddr)");
        query.setString("username", userBean.getUsername());
        query.setString("password", userBean.getPassword());
        query.setString("ipAddr", ipAddr);

        List<UserBean> list = query.addEntity(UserBean.class).list();
        if (!list.isEmpty()) {
            userBean = list.get(0);
            Hibernate.initialize(userBean.getEnterpriseBean());
            Hibernate.initialize(userBean.getRoleBean());
        } else {
            userBean = null;
        }

        session.getTransaction().commit();

    } catch (HibernateException e) {
        session.getTransaction().rollback();
        userBean = null;
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        session.close();
        return userBean;
    }

}

From source file:com.neu.web.phmis.dao.DoctorDao.java

public void createRequest(SurgeryRequestBean surgeryRequestBean) {

    try {//from  w ww.j av a 2  s  .c  o m

        surgeryBean = surgeryRequestBean.getSurgeryBean();
        List<ProductBean> requestedProducts = surgeryRequestBean.getRequestedProducts();

        session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();

        SQLQuery query = session.createSQLQuery(
                "CALL createSurgeryRequest(:userId, :surgeryType, :patientName, :requestNo, :requestTypeId, :requestStatusId, :dateFrom, :dateTo, :instru, :staff)");
        query.setInteger("userId", surgeryBean.getDoctor().getId());
        query.setString("surgeryType", surgeryBean.getType());
        query.setString("patientName", surgeryBean.getPatientName());
        query.setString("requestNo", surgeryRequestBean.getNumber());
        query.setInteger("requestTypeId", surgeryRequestBean.getTypeId());
        query.setInteger("requestStatusId", surgeryRequestBean.getStatusId());
        query.setDate("dateFrom", surgeryRequestBean.getDateFrom());
        query.setDate("dateTo", surgeryRequestBean.getDateTo());
        query.setString("instru", surgeryRequestBean.getSpecialInstructions());
        query.setInteger("staff", surgeryRequestBean.getStaffCount());

        List<Integer> list = query.list();
        surgeryRequestBean.setId(list.get(0));

        for (ProductBean p : requestedProducts) {
            query = session.createSQLQuery("INSERT INTO productrequested VALUES (:requestId, :productId)");
            query.setInteger("requestId", surgeryRequestBean.getId());
            query.setInteger("productId", p.getId());
            query.executeUpdate();
        }

        session.getTransaction().commit();

    } catch (HibernateException e) {
        session.getTransaction().rollback();
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        session.close();
    }

}

From source file:com.neu.web.phmis.dao.InventoryDao.java

public DeviceRequestBean processRequest(DeviceRequestBean deviceRequestBean) {

    try {/*  w  w w  .j  a va 2 s .c o  m*/

        session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        SQLQuery query = session.createSQLQuery(
                "UPDATE request SET statusId = :status, processedDate = NOW(), processComment = :comment WHERE id = :id");
        query.setInteger("status", deviceRequestBean.getStatusId());
        query.setString("comment", deviceRequestBean.getProcessComment());
        query.setInteger("id", deviceRequestBean.getId());
        query.executeUpdate();
        deviceRequestBean = (DeviceRequestBean) session.get(DeviceRequestBean.class, deviceRequestBean.getId());
        session.getTransaction().commit();

    } catch (HibernateException e) {
        session.getTransaction().rollback();
        deviceRequestBean = null;
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        session.close();
        return deviceRequestBean;
    }

}

From source file:com.neu.web.phmis.dao.InventoryDao.java

public void addOrder(List<OrderBean> orderList) {

    try {//  w  ww. j  av  a 2s.  co m

        session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();

        for (OrderBean ob : orderList) {
            SQLQuery query = session.createSQLQuery("CALL addOrder(:1,:2,:3)");
            query.setString("1", ob.getNumber());
            query.setInteger("2", ob.getOrderedBy().getId());
            query.setInteger("3", ob.getOrderedFrom().getId());
            List<OrderItemBean> temp = ob.getOrderItemList();
            ob = (OrderBean) query.addEntity(OrderBean.class).uniqueResult();
            ob.setOrderItemList(temp);
            for (OrderItemBean oib : ob.getOrderItemList()) {
                query = session.createSQLQuery(
                        "INSERT INTO orderitem (orderId, productId, quantity) VALUES (:1, :2, :3)");
                query.setInteger("1", ob.getId());
                query.setInteger("2", oib.getProductBean().getId());
                query.setInteger("3", oib.getQuantity());
                query.executeUpdate();
            }
        }

        session.getTransaction().commit();

    } catch (HibernateException e) {
        session.getTransaction().rollback();
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        session.close();
    }

}

From source file:com.neu.web.phmis.dao.ResourceDao.java

public int getRequestCounts() {

    int counts = 0;

    try {//from  ww  w.  j av a  2  s  .c  o  m

        session = HibernateUtil.getSessionFactory().openSession();
        SQLQuery query = session.createSQLQuery(
                "SELECT COUNT(id) FROM request WHERE typeId = (SELECT typeId FROM requestType WHERE type = :type)");
        query.setString("type", RequestBean.SURGERY);
        List<BigInteger> list = query.list();
        counts = list.get(0).intValue();

    } catch (HibernateException e) {
        session.getTransaction().rollback();
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        session.close();
        return counts;
    }

}

From source file:com.neu.web.phmis.dao.ResourceDao.java

public void createRequest(DeviceRequestBean deviceRequestBean) {

    try {// w  w w. j av a 2  s .co  m

        session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        SQLQuery query = session.createSQLQuery("CALL createDeviceRequest(:1, :2, :3, :4, :5, :6)");
        query.setInteger("1", deviceRequestBean.getCreatedBy().getId());
        query.setString("2", deviceRequestBean.getNumber());
        query.setInteger("3", deviceRequestBean.getTypeId());
        query.setInteger("4", deviceRequestBean.getStatusId());
        query.setInteger("5", deviceRequestBean.getWarehouse().getId());
        query.setString("6", deviceRequestBean.getSpecialInstruction());
        List<Integer> list = query.list();
        deviceRequestBean.setId(list.get(0));

        for (ProductBean p : deviceRequestBean.getRequestedProducts()) {
            query = session.createSQLQuery("INSERT INTO productrequested VALUES (:requestId, :productId)");
            query.setInteger("requestId", deviceRequestBean.getId());
            query.setInteger("productId", p.getId());
            query.executeUpdate();
        }

        session.getTransaction().commit();

    } catch (HibernateException e) {
        session.getTransaction().rollback();
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        session.close();
    }

}

From source file:com.neu.web.phmis.dao.ResourceDao.java

public SurgeryRequestBean processRequest(SurgeryRequestBean surgeryRequestBean) {

    try {/*from ww w .  ja va2 s.  c  o  m*/

        session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        SQLQuery query = session.createSQLQuery(
                "UPDATE request SET statusId = :status, processedDate = NOW(), processComment = :comment WHERE id = :id");
        query.setInteger("status", surgeryRequestBean.getStatusId());
        query.setString("comment", surgeryRequestBean.getProcessComment());
        query.setInteger("id", surgeryRequestBean.getId());
        query.executeUpdate();
        surgeryRequestBean = (SurgeryRequestBean) session.get(SurgeryRequestBean.class,
                surgeryRequestBean.getId());
        session.getTransaction().commit();

    } catch (HibernateException e) {
        session.getTransaction().rollback();
        surgeryRequestBean = null;
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        session.close();
        return surgeryRequestBean;
    }

}

From source file:com.openkm.dao.AuthDAO.java

License:Open Source License

/**
 * Delete role from database//w w w  .j a va 2  s . c  o  m
 */
public static void deleteRole(String rolId) throws DatabaseException, AccessDeniedException {
    log.debug("deleteRole({})", rolId);
    // Check if user has enought grants for the action
    checkAccessGrants(null);

    String qs = "delete from OKM_USER_ROLE where UR_ROLE=:rolId";
    Session session = null;
    Transaction tx = null;

    try {
        session = HibernateUtil.getSessionFactory().openSession();
        tx = session.beginTransaction();
        Role role = (Role) session.load(Role.class, rolId);
        session.delete(role);

        // TODO: Make Hibernate handle this relation.
        SQLQuery q = session.createSQLQuery(qs);
        q.setString("rolId", rolId);
        q.executeUpdate();

        HibernateUtil.commit(tx);
    } catch (HibernateException e) {
        HibernateUtil.rollback(tx);
        throw new DatabaseException(e.getMessage(), e);
    } finally {
        HibernateUtil.close(session);
    }

    log.debug("deleteRole: void");
}