Example usage for org.hibernate Query setInteger

List of usage examples for org.hibernate Query setInteger

Introduction

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

Prototype

@Deprecated
@SuppressWarnings("unchecked")
default Query<R> setInteger(String name, int val) 

Source Link

Document

Bind a named int-valued parameter.

Usage

From source file:com.hp.j2sh.project.action.RegisterCommunity.java

@Override
public String execute() throws Exception {
    try {/* ww w .  j a va 2  s  . c o m*/
        Integer user_id = (Integer) sessionMap.get("user_id");
        Session session = MyHibernateUtil.getSession();
        Transaction transaction = session.beginTransaction();

        Query query = session.createQuery("FROM AllUsers a WHERE a.userId = :userID");
        query.setInteger("userID", user_id);
        AllUsers allUsers = (AllUsers) query.uniqueResult();

        AllCommunities allCommunities = new AllCommunities(community_name, user_id);
        allCommunities.setAllUsers(allUsers);
        allUsers.getAllCommunities().add(allCommunities);

        session.save(allCommunities);
        session.save(allUsers);
        transaction.commit();

        sessionMap.put("RegisterMessage", "Community Creation Successful !!!");

        query = session.createQuery("FROM CommunityUsers c WHERE c.userId = ?");
        query.setInteger(0, (Integer) sessionMap.get("user_id"));
        System.out.println("query :: " + query.getQueryString());
        List<CommunityUsers> tempList = query.list();
        List<AllCommunities> listUser = new ArrayList<AllCommunities>();
        for (CommunityUsers communityUsers1 : tempList) {
            Criteria criteria2 = session.createCriteria(AllCommunities.class);
            criteria2.add(Restrictions.eq("communityId", communityUsers1.getCommunityId()));
            AllCommunities allCommunities1 = (AllCommunities) criteria2.uniqueResult();
            listUser.add(allCommunities1);
        }
        sessionMap.put("communityListUser", listUser);

        List<AllCommunities> listAll = new ArrayList<AllCommunities>();
        List<Integer> userIds = new ArrayList<Integer>();
        for (AllCommunities allCommunities1 : listUser) {
            userIds.add(allCommunities1.getCommunityId());
        }
        System.out.println("userIDs :: " + userIds);
        Criteria criteria1 = session.createCriteria(AllCommunities.class);
        List<AllCommunities> tempListAll = criteria1.list();
        for (AllCommunities ac : tempListAll) {
            boolean found = false;
            for (Integer i : userIds) {
                if (ac.getCommunityId().equals(i)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                listAll.add(ac);
            }
        }
        sessionMap.put("communityListAll", listAll);
        return SUCCESS;
    } catch (Exception e) {
        System.out.println("( RegisterCommunity ) Exception :: " + e);
    }
    sessionMap.put("RegisterMessage", "Community Creation Failed !!!");
    return ERROR;
}

From source file:com.hp.j2sh.project.interceptor.CommunityRetrieval.java

public String intercept(ActionInvocation actionInvocation) throws Exception {
    Session session = MyHibernateUtil.getSession();

    Query query = session.createQuery("FROM CommunityUsers c WHERE c.userId = :user_id");
    query.setInteger("user_id", (Integer) sessionMap.get("user_id"));
    List<CommunityUsers> tempList = query.list();
    List<AllCommunities> listUser = null;
    for (CommunityUsers communityUsers : tempList) {
        Criteria criteria2 = session.createCriteria(AllCommunities.class);
        criteria2.add(Restrictions.eq("communityId", communityUsers.getCommunityId()));
        AllCommunities allCommunities = (AllCommunities) criteria2.uniqueResult();
        listUser.add(allCommunities);/*ww w .java 2 s .  c  o m*/
    }
    sessionMap.put("communityListUser", listUser);

    List<AllCommunities> listAll = null;
    List<Integer> userIds = null;
    for (AllCommunities allCommunities : listUser) {
        userIds.add(allCommunities.getCommunityId());
    }
    Criteria criteria1 = session.createCriteria(AllCommunities.class);
    List<AllCommunities> tempListAll = criteria1.list();
    for (AllCommunities ac : tempListAll) {
        boolean found = false;
        for (Integer i : userIds) {
            if (ac.getCommunityId() == i) {
                found = true;
                break;
            }
        }
        if (!found) {
            listAll.add(ac);
        }
    }
    sessionMap.put("communityListAll", listAll);

    actionInvocation.invoke();

    return "";
}

From source file:com.humber.java.dao.AlbumDAO.java

public Album getAlbumById(int albumId) {
    Album album = null;/*from  ww w.  j a  v a 2 s . c  o m*/
    Transaction trns = null;
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        trns = session.beginTransaction();
        String queryString = "from Album where id = :id";
        Query query = session.createQuery(queryString);
        query.setInteger("id", albumId);
        album = (Album) query.uniqueResult();
    } catch (RuntimeException e) {
        e.printStackTrace();
    } finally {
        session.clear();
        session.flush();
        session.close();
    }
    return album;
}

From source file:com.humber.java.dao.ImageDAO.java

public Picture getImageById(int imageId) {
    Picture image = null;/*from  ww  w .  ja v a 2s  .  c  o  m*/
    Transaction trns = null;
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        trns = session.beginTransaction();
        String queryString = "from Image where id = :id";
        Query query = session.createQuery(queryString);
        query.setInteger("id", imageId);
        image = (Picture) query.uniqueResult();
    } catch (RuntimeException e) {
        e.printStackTrace();
    } finally {
        session.flush();
        session.close();
    }
    return image;
}

From source file:com.ikon.dao.DashboardDAO.java

License:Open Source License

/**
 * Get dashboard stats//from  w w  w .  j  a v a2 s  . c  om
 */
@SuppressWarnings("unchecked")
public Dashboard findByPk(int dsId) throws DatabaseException {
    log.debug("findByPk({})", dsId);
    String qs = "from Dashboard db where db.id=:id";
    Session session = null;

    try {
        session = HibernateUtil.getSessionFactory().openSession();
        Query q = session.createQuery(qs);
        q.setInteger("id", dsId);
        List<Dashboard> results = q.list(); // uniqueResult
        Dashboard ret = null;

        if (results.size() == 1) {
            ret = results.get(0);
        }

        log.debug("findByPk: {}", ret);
        return ret;
    } catch (HibernateException e) {
        throw new DatabaseException(e.getMessage(), e);
    } finally {
        HibernateUtil.close(session);
    }
}

From source file:com.ikon.dao.StapleGroupDAO.java

License:Open Source License

/**
 * Find by pk//from w  ww  .jav  a2  s . c  o  m
 */
public static StapleGroup findByPk(int sgId) throws DatabaseException {
    log.debug("findByPk({})", sgId);
    String qs = "from StapleGroup sg where sg.id=:id";
    Session session = null;

    try {
        session = HibernateUtil.getSessionFactory().openSession();
        Query q = session.createQuery(qs);
        q.setInteger("id", sgId);
        StapleGroup ret = (StapleGroup) q.setMaxResults(1).uniqueResult();
        log.debug("findByPk: {}", ret);
        return ret;
    } catch (HibernateException e) {
        throw new DatabaseException(e.getMessage(), e);
    } finally {
        HibernateUtil.close(session);
    }
}

From source file:com.imos.hibernate.HibernateQueryTest.java

public static void main(String[] args) {
    try (SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory()) {
        System.out.println("\nSelect \n");
        try (Session session = sessionFactory.openSession()) {
            session.beginTransaction();//  w w w.j  a  v  a 2 s.  com

            //                Query query = session.createQuery("from Vehicle where vehicleType = 0");
            Query query;
            query = session.createQuery("from PersonDetails");
            List<PersonDetails> list = query.list();
            list.stream().forEach((pd) -> {
                System.out.println("Object : " + pd);
                System.out.println("Name : " + pd.getUserName());
                System.out.println("Vehicle : " + pd.getVehicles());
                System.out.println("CC : " + pd.getCommunityCenters());
                System.out.println("Company : " + pd.getCompany());
                System.out.println("Address : " + pd.getAddresses());
            });

            //                query = session.createQuery("from Vehicle where vehicleType= :value");
            query = session.getNamedQuery("Vehicle.getByVehicleType");
            //                query.setInteger(0, 0);
            query.setInteger("vehicleType", Integer.parseInt("0"));
            List<Vehicle> vlist = query.list();
            System.out.println(vlist);
        } catch (Exception e) {
            System.out.println(e.getCause().toString() + " : " + e.getMessage());
        }
    } catch (Exception e) {
        System.out.println(e.getCause().toString() + " : " + e.getMessage());
    }
}

From source file:com.jc.campitos.capitulo2.DAOPruebaImpl.java

public Prueba buscarPorId(int id) {
    begin();/* w  ww  . j  a  v  a  2  s.  c o  m*/
    Query q = getSession().createQuery("from Prueba where id=:id");
    q.setInteger("id", id);
    Prueba p = (Prueba) q.uniqueResult();
    commit();
    close();
    return p;
}

From source file:com.jc.campitos.DAOClienteImpl.java

/**
 * Este metodo busca un cliente por medio de su Id
 * @param id Este parametro es el Id del cliente que se quiere buscar
 * @return El tipo de retorno es el cliente buscado
 *///from  w  ww  .  ja va  2s. c o  m
public Cliente buscarPorId(int id) {
    begin();
    Query q = getSession().createQuery("from Cliente where id = :id");
    q.setInteger("id", id);
    Cliente p = (Cliente) q.uniqueResult();
    commit();
    close();
    return p;

}

From source file:com.juzhituan.dao.impl.EmployerDaoHibernateImpl.java

@Override
public Employer findEmployer(final Integer employerId, final String password) {

    return getHibernateTemplate().execute(new HibernateCallback<Employer>() {
        @Override/*w w  w.  j  ava  2 s .c o m*/
        public Employer doInHibernate(Session session) throws HibernateException {
            String hql = "from Employer where employerId=:employerId and password=:password";
            Query query = session.createQuery(hql);
            query.setInteger("employerId", employerId);
            query.setString("password", password);
            Employer employer = (Employer) query.uniqueResult();
            return employer;
        }
    });
}