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:beans.NastavniciControler.java

public void dohvKurseve() {

    session = dbFactory.getFactory().openSession();

    Query q = session.createQuery("FROM Predavac_kurs WHERE predavac=:id");
    q.setParameter("id", nastavnik.getIDKor());
    kursevi_drzi = q.list();/*from   ww  w  .  j  a  v  a  2s .co m*/
    session.close();
    if (kursevi_drzi.size() > 0) {

        dohvSveKurseve();

        for (int i = 0; i < kursevi_drzi.size(); i++) {
            for (int j = 0; j < kurseviSvi.size(); j++) {
                if (kursevi_drzi.get(i).getKurs() == kurseviSvi.get(j).getIDKurs()) {
                    kursevi.add(kurseviSvi.get(j));
                }
            }
        }

        for (int i = 0; i < kursevi.size(); i++) {
            if (kursevi.get(i).getTip().equals("izborni")) {
                izborni.add(kursevi.get(i));
            } else {
                obavezni.add(kursevi.get(i));
            }
        }
    }

}

From source file:beans.PredmetControler.java

public String ucitajPredmet(String nazivPredmeta) {
    session = dbFactory.getFactory().openSession();
    Query q = session.createQuery("FROM Kurs WHERE nazivKursa=:naz");
    q.setParameter("naz", nazivPredmeta);
    List<Kurs> result = q.list();
    session.close();/*from w w  w.ja  v  a2 s .  c  o m*/
    if (result.size() > 0) {
        kurs = result.get(0);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("kurs", kurs);

    } else {
        poruka = "Stranica predmeta je u izradi";
        return "informacijeKurs.xhtml";
    }

    session = dbFactory.getFactory().openSession();
    q = session.createQuery("FROM Obavestenje_kurs WHERE kurs=:k");
    q.setParameter("k", kurs.getIDKurs());
    listaObavestenja = q.list();
    session.close();

    List<Integer> idObavestenja = new ArrayList<Integer>();
    for (int i = 0; i < listaObavestenja.size(); i++) {
        idObavestenja.add(listaObavestenja.get(i).getObavestenje());
    }

    List<Obavestenje> obavestenjaSva = new ArrayList<Obavestenje>();
    session = dbFactory.getFactory().openSession();
    q = session.createQuery("FROM Obavestenje WHERE arhivirano=0");
    obavestenjaSva = q.list();
    session.close();

    for (int i = 0; i < idObavestenja.size(); i++) {
        for (int j = 0; j < obavestenjaSva.size(); j++) {
            if (idObavestenja.get(i) == obavestenjaSva.get(j).getIDObavestenja()) {
                obavestenja.add(obavestenjaSva.get(j));
            }
        }
    }

    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("obavestenjaKurs", obavestenja);
    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("kurs", kurs);

    petObavestenja.clear();
    ukupnoStrana = obavestenja.size() / 5;
    strana = 1;
    if (obavestenja.size() % 5 > 0) {
        ukupnoStrana++;
    }
    if (obavestenja.size() >= 5) {
        for (int i = 0; i < 5; i++) {
            petObavestenja.add(obavestenja.get(i));
        }
    } else {
        for (int i = 0; i < obavestenja.size(); i++) {
            petObavestenja.add(obavestenja.get(i));
        }
    }

    session = dbFactory.getFactory().openSession();
    q = session.createQuery("FROM Kurs_stavke WHERE kurs=:id");
    q.setParameter("id", kurs.getIDKurs());
    stavkeMenija = q.list();
    session.close();

    Korisnik kor = (Korisnik) FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
            .get("korisnik");
    session = dbFactory.getFactory().openSession();
    q = session.createQuery("FROM Predavac_kurs WHERE predavac=:p");
    q.setParameter("p", kor.getIDKor());
    List<Predavac_kurs> pk = q.list();
    session.close();

    if (pk.size() > 0) {
        for (int i = 0; i < pk.size(); i++) {
            if (pk.get(i).getKurs() == kurs.getIDKurs()) {
                flagPredaje = true;
                break;
            }
        }
    }

    return "kurs_index.xhtml";
}

From source file:beans.PredmetControler.java

public void pretraziObavestenja(ValueChangeEvent e) {

    if (e.getNewValue() != null) {
        List<Obavestenje> obavestenjaKurs = new ArrayList<Obavestenje>();
        if (e.getNewValue().equals("sve")) {
            petObavestenja.clear();/* w ww  .j a  v a2s . co  m*/
            obavestenjaKurs = (List<Obavestenje>) FacesContext.getCurrentInstance().getExternalContext()
                    .getSessionMap().get("obavestenjaKurs");
        } else {
            petObavestenja.clear();
            List<Obavestenje> obavestenjaTemp = (List<Obavestenje>) FacesContext.getCurrentInstance()
                    .getExternalContext().getSessionMap().get("obavestenjaKurs");
            for (int i = 0; i < obavestenjaTemp.size(); i++) {
                if (obavestenjaTemp.get(i).getTip().equals(e.getNewValue())) {
                    obavestenjaKurs.add(obavestenjaTemp.get(i));
                }
            }
        }
        if (obavestenjaKurs.size() > 0) {
            ukupnoStrana = obavestenjaKurs.size() / 5;
        } else {
            ukupnoStrana = 1;
        }
        strana = 1;
        if (obavestenjaKurs.size() % 5 > 0) {
            ukupnoStrana++;
        }
        if (obavestenjaKurs.size() >= 5) {
            for (int i = 0; i < 5; i++) {
                petObavestenja.add(obavestenjaKurs.get(i));
            }
        } else {
            for (int i = 0; i < obavestenjaKurs.size(); i++) {
                petObavestenja.add(obavestenjaKurs.get(i));
            }
        }

        Kurs k = (Kurs) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("kurs");

        session = dbFactory.getFactory().openSession();
        Query q = session.createQuery("FROM Kurs_stavke WHERE kurs=:id");
        q.setParameter("id", k.getIDKurs());
        stavkeMenija = q.list();
        session.close();

    }
}

From source file:beans.PredmetControler.java

public void promeniStranu(ValueChangeEvent e) {
    List<Obavestenje> obavestenjaKurs = new ArrayList<Obavestenje>();
    if (tipObavestenja == null || tipObavestenja.equals("") || tipObavestenja.equals("sve")) {
        obavestenjaKurs = (List<Obavestenje>) FacesContext.getCurrentInstance().getExternalContext()
                .getSessionMap().get("obavestenjaKurs");
    } else {/*from w  ww.  ja  v a2 s .  c  o  m*/
        List<Obavestenje> obavestenjaTemp = (List<Obavestenje>) FacesContext.getCurrentInstance()
                .getExternalContext().getSessionMap().get("obavestenjaKurs");
        for (int i = 0; i < obavestenjaTemp.size(); i++) {
            if (obavestenjaTemp.get(i).getTip().equals(e.getNewValue())) {
                obavestenjaKurs.add(obavestenjaTemp.get(i));
            }
        }
    }
    petObavestenja.clear();
    strana = (int) e.getNewValue();
    if (strana < ukupnoStrana) {
        for (int i = (strana * 5) - 5; i < (strana * 5); i++) {
            petObavestenja.add(obavestenjaKurs.get(i));
        }
    } else {
        for (int i = (strana * 5) - 5; i < obavestenjaKurs.size(); i++) {
            petObavestenja.add(obavestenjaKurs.get(i));
        }
    }

    Kurs k = (Kurs) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("kurs");
    session = dbFactory.getFactory().openSession();
    Query q = session.createQuery("FROM Kurs_stavke WHERE kurs=:id");
    q.setParameter("id", k.getIDKurs());
    stavkeMenija = q.list();
    session.close();

}

From source file:beans.PredmetControler.java

public String materijali() {
    Kurs k = (Kurs) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("kurs");
    session = dbFactory.getFactory().openSession();
    Query q = session.createQuery("FROM Materijal WHERE kurs=:id");
    q.setParameter("id", k.getIDKurs());
    materijali = q.list();/*from   w w w  .  j a v  a 2  s  .  c  o m*/
    session.close();

    for (int i = 0; i < materijali.size(); i++) {
        if (materijali.get(i).getTip().equals("predavanja")) {
            materijaliPredavanja.add(materijali.get(i));
        } else {
            materijaliVezbe.add(materijali.get(i));
        }
    }

    return "kurs_materijali.xhtml";
}

From source file:beans.PredmetControler.java

public void ucitajMaterijale() {
    Kurs k = (Kurs) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("kurs");
    session = dbFactory.getFactory().openSession();
    Query q = session.createQuery("FROM Materijal WHERE kurs=:id");
    q.setParameter("id", k.getIDKurs());
    materijali = q.list();/*from  ww  w .  j  av  a2 s.co m*/
    session.close();

    for (int i = 0; i < materijali.size(); i++) {
        if (materijali.get(i).getTip().equals("predavanja")) {
            materijaliPredavanja.add(materijali.get(i));
        } else {
            materijaliVezbe.add(materijali.get(i));
        }
    }
}

From source file:beans.PredmetControler.java

public void dodajObavestenje() {

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

    Query q = session.createQuery("SELECT naziv FROM Obavestenje WHERE naziv=:naz");
    q.setParameter("naz", obavestenjeNovo.getNaziv());
    List results = q.list();/*w ww  . j av  a2 s  . c o m*/

    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();
        obavestenjeNovo.setDatum(datum);
        session.save(obavestenjeNovo);
        session.getTransaction().commit();
        session.close();

        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", kurs.getNazivKursa());
            //                List<Kurs> tempKurs = q.list();
            //                Kurs k = new Kurs();
            //                if (tempKurs.size() > 0) {
            //                    k = tempKurs.get(0);
            //                }
            //                session.close();
            ok.setKurs(kursTekuci.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", ""));

    }
}

From source file:benchmark.BenchmarkHnP.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 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;//w  w  w.  jav a2  s  . c o  m
    for (HbmOrderItem 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.BenchmarkHnP.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 HbmOrder where id = :id and deleted = :deleted";
        Query query = session.createQuery(hql);
        query.setParameter("id", new Long(i));
        query.setParameter("deleted", true);
        List<HbmOrder> items = (List<HbmOrder>) query.list();
    }//w w  w  .  j a  va 2s. c  o  m

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

}

From source file:benchmark.BenchmarkHnP.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 HbmOrder where deleted = :deleted";
    Query query = session.createQuery(hql);
    query.setParameter("deleted", false);
    List<HbmOrder> orders = (List<HbmOrder>) query.list();

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

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

        for (HbmOrderItem 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());
}