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:baking.dao.BaseDao.java

License:Open Source License

/**
 * ?count(*)?hql??//from  w w w  .  ja va 2s.  c om
 * 
 * @param hql the hql
 * 
 * @return the toatal by hql
 */
public int countHql(String hql, Object value) {
    //
    Query query = getSession().createQuery(hql);
    //??
    query.setParameter(0, value);
    Long result = (Long) query.uniqueResult();
    if (result != null) {
        return result.intValue();
    }
    return 0;
    //return total;
}

From source file:baking.dao.BaseDao.java

License:Open Source License

/**
 * ?count(*)?hql??/*from   w ww .  j  a  va  2  s  .  c  o  m*/
 * 
 * @param hql the hql
 * 
 * @return the toatal by hql
 */
public int countHql(String hql, Object[] values) {
    //
    Query query = getSession().createQuery(hql);
    //??
    if (values != null && values.length > 0) {
        for (int i = 0; i < values.length; i++)
            query.setParameter(i, values[i]);
    }
    Long result = (Long) query.uniqueResult();
    if (result != null) {
        return result.intValue();
    }
    return 0;
}

From source file:baking.dao.BaseDao.java

License:Open Source License

/**
 * ?hql/*from  w  w w .  ja  va2 s  . c om*/
 * @description   
 * @author  JiaCao
 * @param hql
 * @param values  
 */
@SuppressWarnings("unchecked")
public void executeHql(final String hql, final Object[] values) {
    try {
        Session session = getSession();
        Query query = session.createQuery(hql);
        for (int i = 0; i < values.length; i++) {
            query.setParameter(i, values[i]);
        }
        query.executeUpdate();
    } catch (RuntimeException re) {
        throw re;
    }
}

From source file:beans.Administrator.java

public void dodajObavestenje() {

    Korisnik kor = (Korisnik) FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
            .get("korisnik");
    session = dbFactory.getFactory().openSession();

    Query q = session.createQuery("SELECT naziv FROM Obavestenje WHERE naziv=:naz");
    q.setParameter("naz", obavestenje.getNaziv());
    List results = q.list();/*from  www.  j a  v a 2 s.com*/

    long vreme = System.currentTimeMillis();

    Calendar datum = Calendar.getInstance();
    datum.setTimeInMillis(vreme);

    if (results.size() > 0) {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                "Obavetenje sa istim nazivom ve postoji", ""));
        session.close();
        return;
    } else {
        session.beginTransaction();
        obavestenje.setDatum(datum);
        session.save(obavestenje);
        session.getTransaction().commit();
        session.close();
        if (!kursObavestenje.equals("katedra")) {
            session = dbFactory.getFactory().openSession();
            q = session.createQuery("FROM Obavestenje");
            List<Obavestenje> temp = q.list();
            session.close();
            int idObavestenje;
            if (temp.size() > 0) {
                idObavestenje = temp.get(temp.size() - 1).getIDObavestenja();
                Obavestenje_kurs ok = new Obavestenje_kurs();

                session = dbFactory.getFactory().openSession();
                q = session.createQuery("FROM Kurs WHERE nazivKursa=:naz");
                q.setParameter("naz", kursObavestenje);
                List<Kurs> tempKurs = q.list();
                Kurs k = new Kurs();
                if (tempKurs.size() > 0) {
                    k = tempKurs.get(0);
                }
                session.close();

                ok.setKurs(k.getIDKurs());
                ok.setObavestenje(idObavestenje);
                session = dbFactory.getFactory().openSession();
                session.beginTransaction();
                session.save(ok);
                session.getTransaction().commit();
                session.close();
            }
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                    "Obavetenje je uspeno dodato, moete ga videti na stranici predmeta", ""));
        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                    "Obavetenje je uspeno dodato, moete ga pogledati u sekciji obavetenja", ""));
        }

    }
}

From source file:beans.Administrator.java

public void dodajKurs() {
    session = dbFactory.getFactory().openSession();
    Query q = session.createQuery("FROM Kurs WHERE nazivKursa=:naz");
    q.setParameter("naz", kurs.getNazivKursa());
    List<Kurs> results = q.list();
    if (results.size() > 0) {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_ERROR, "Kurs sa istim nazivom ve postoji", ""));
        session.close();//from   w w  w  .j a  va  2s  .  c om
        return;
    } else {
        session.beginTransaction();
        session.save(kurs);
        session.getTransaction().commit();
        if (predavaci.getTarget().size() > 0) {
            int id;
            Predavac_kurs pk = new Predavac_kurs();
            q = session.createQuery("FROM Kurs WHERE nazivKursa=:naz");
            q.setParameter("naz", kurs.getNazivKursa());
            results = q.list();
            if (results.size() > 0) {
                id = results.get(0).getIDKurs();
                odabraniPredavaci = predavaci.getTarget().toString();
                String subString = odabraniPredavaci.substring(1, odabraniPredavaci.length() - 1);

                odabraniPredavaciNiz = new String[predavaci.getTarget().size()];
                odabraniPredavaciNiz = subString.split(", ");

                //                    q = session.createQuery("FROM Korisnik WHERE tip=1");
                //                    List<Korisnik> res = q.list();
                for (int i = 0; i < odabraniPredavaciNiz.length; i++) {
                    int idPredavac = Integer.parseInt(odabraniPredavaciNiz[i]);
                    session = dbFactory.getFactory().openSession();
                    session.beginTransaction();
                    pk.setPredavac(idPredavac);
                    pk.setKurs(id);
                    session.save(pk);
                    session.getTransaction().commit();
                    session.close();
                }
                Kurs_stavke ks = new Kurs_stavke();
                if (stavke.getTarget().size() > 0) {

                    for (int i = 0; i < stavke.getTarget().size(); i++) {
                        session = dbFactory.getFactory().openSession();
                        session.beginTransaction();
                        ks.setKurs(id);
                        ks.setStavka(stavke.getTarget().get(i));
                        session.save(ks);
                        session.getTransaction().commit();
                        session.close();
                    }

                } else {
                    for (int i = 0; i < stavke.getSource().size(); i++) {
                        session = dbFactory.getFactory().openSession();
                        session.beginTransaction();
                        ks.setKurs(id);
                        ks.setStavka(stavke.getSource().get(i));
                        session.save(ks);
                        session.getTransaction().commit();
                        session.close();
                    }
                }

            }
            //                session.close();
        }
        flag = true;
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                "Uspesno je dodat kurs, sada mozete dodati i materijale za tekuci kurs", ""));
        return;
    }

}

From source file:beans.Administrator.java

public void mojiPredmeti() {

    Korisnik kor = (Korisnik) FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
            .get("korisnik");

    session = dbFactory.getFactory().openSession();
    Query q = session.createQuery("FROM Predavac_kurs WHERE predavac=:id");
    q.setParameter("id", kor.getIDKor());
    List<Predavac_kurs> temp_drzi = q.list();
    session.close();/*from   w w w  . j a  va 2s .co  m*/
    if (temp_drzi.size() > 0) {

        session = dbFactory.getFactory().openSession();
        q = session.createQuery("FROM Kurs");
        List<Kurs> kurseviSvi = q.list();
        session.close();

        mojiPredmetiNiz.clear();
        for (int i = 0; i < temp_drzi.size(); i++) {
            for (int j = 0; j < kurseviSvi.size(); j++) {
                if (temp_drzi.get(i).getKurs() == kurseviSvi.get(j).getIDKurs()) {
                    mojiPredmetiNiz.add(kurseviSvi.get(j));
                }
            }
        }
        mojiPredmetiNazivi.clear();
        for (int i = 0; i < mojiPredmetiNiz.size(); i++) {
            mojiPredmetiNazivi.add(mojiPredmetiNiz.get(i).getNazivKursa());
        }
    }
    //        return "dodavanjeObavestenja.xhtml";
}

From source file:beans.Cart.java

/**
 * Find the user from the database according to the given username and
 * password and set into the client field.
 *///from   ww w.  ja v  a  2 s . c  om
public void findClient() {
    if (loginData.getUsername() != null || loginData.getPassword() != null) {
        Session session = hibernate.HibernateUtil.getSessionFactory().openSession();
        List<Client> clients;
        Query query = session.createQuery("FROM Client WHERE username =:username AND password =:password");
        query.setParameter("username", loginData.getUsername());
        query.setParameter("password", loginData.getPassword());
        clients = query.list();
        if (clients.size() > 0) {
            client = clients.get(0);
        }
        session.close();
    }
}

From source file:beans.Controller.java

public void pretraziObavestenja(ValueChangeEvent e) {
    if (e.getNewValue() != null) {
        if (e.getNewValue().equals("sve")) {
            petObavestenja.clear();/*  w ww.ja v  a2s.c  o  m*/
            session = dbFactory.getFactory().openSession();
            Query q = session.createQuery("FROM Obavestenje WHERE arhivirano=0");
            obavestenjaSva = q.list();
            session.close();
        } else {
            petObavestenja.clear();
            session = dbFactory.getFactory().openSession();
            Query q = session.createQuery("FROM Obavestenje WHERE tip=:t AND arhivirano=0");
            q.setParameter("t", e.getNewValue());
            obavestenjaSva = q.list();
            session.close();
        }
        ukupnoStrana = obavestenjaSva.size() / 5;
        strana = 1;
        if (obavestenjaSva.size() % 5 > 0) {
            ukupnoStrana++;
        }
        if (obavestenjaSva.size() >= 5) {
            for (int i = 0; i < 5; i++) {
                petObavestenja.add(obavestenjaSva.get(i));
            }
        } else {
            for (int i = 0; i < obavestenjaSva.size(); i++) {
                petObavestenja.add(obavestenjaSva.get(i));
            }
        }

    }

}

From source file:beans.Controller.java

public void promeniStranu(ValueChangeEvent e) {
    if (tipObavestenja == null || tipObavestenja.equals("") || tipObavestenja.equals("sve")) {
        session = dbFactory.getFactory().openSession();
        Query q = session.createQuery("FROM Obavestenje WHERE arhivirano=0");
        obavestenjaSva = q.list();//from w ww.  j  av  a2 s.  com
        session.close();
    } else {
        session = dbFactory.getFactory().openSession();
        Query q = session.createQuery("FROM Obavestenje WHERE tip=:t AND arhivirano=0");
        q.setParameter("t", tipObavestenja);
        obavestenjaSva = q.list();
        session.close();
    }
    petObavestenja.clear();
    strana = (int) e.getNewValue();
    if (strana < ukupnoStrana) {
        int j = 0;
        for (int i = (strana * 5) - 5; i < (strana * 5); i++) {
            petObavestenja.add(obavestenjaSva.get(i));
            j++;
        }
    } else {
        int j = 0;
        for (int i = (strana * 5) - 5; i < obavestenjaSva.size(); i++) {
            petObavestenja.add(obavestenjaSva.get(i));
            j++;
        }
    }
}

From source file:beans.NastavniciControler.java

public String detalji(String ime) {

    String[] niz = new String[2];
    if (ime != null && !ime.equals("")) {
        niz = ime.split("_");
        session = dbFactory.getFactory().openSession();
        Query q = session.createQuery("FROM Korisnik WHERE ime=:i AND prezime=:p");
        q.setParameter("i", niz[0]);
        q.setParameter("p", niz[1]);

        List<Korisnik> temp = new ArrayList<Korisnik>();
        temp = q.list();//  w w w . j  a v a  2  s.  c  om
        session.close();
        if (temp.size() > 0) {
            nastavnik = temp.get(0);
        } else {
            poruka = "Ne postoji stranica za trazenog profesora";
            return "informacijeProfesor.xhtml";
        }
    }
    return "nastavnikDetalji.xhtml";
}