Example usage for org.hibernate Query list

List of usage examples for org.hibernate Query list

Introduction

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

Prototype

List<R> list();

Source Link

Document

Return the query results as a List.

Usage

From source file:bean.session.SesionClienteBean.java

public boolean validar() {
    Usuarios claseUsuario = null;//from   w w  w.  j a  v a  2s . c o m
    Session session = null;
    try {
        session = HibernateUtil.getSessionFactory().openSession();
        String hql = "select u from Usuarios u, Clientes c where (u.clientes.idCliente=c.idCliente) and (u.usuario= '"
                + this.getUsuario() + "' and u.pass = '" + this.getContrasenia() + "'";
        Query query = session.createQuery(hql);

        if (!query.list().isEmpty()) {
            claseUsuario = (Usuarios) query.list().get(0);

        }

    } catch (Exception e) {
        throw e;
    } finally {
        if (session == null) {
            session.close();

        }
    }
    if (claseUsuario != null) {
        HttpSession miSession = (HttpSession) FacesContext.getCurrentInstance().getExternalContext()
                .getSession(true);
        miSession.setAttribute("usuario", claseUsuario);
        return true;
    } else
        return false;

}

From source file:bean.session.SesionGerenteBean.java

public boolean validar() {
    Usuarios claseUsuario = null;/*from   w ww . java  2 s  . c o m*/
    Session session = null;
    try {
        session = HibernateUtil.getSessionFactory().openSession();
        String hql = "FROM Usuarios WHERE usuario = '" + this.getUsuario() + "' and pass = '"
                + this.getContrasenia() + "'";
        Query query = session.createQuery(hql);

        if (!query.list().isEmpty()) {
            claseUsuario = (Usuarios) query.list().get(0);

        }

    } catch (Exception e) {
        throw e;
    } finally {
        if (session == null) {
            session.close();

        }
    }
    if (claseUsuario != null) {
        HttpSession miSession = (HttpSession) FacesContext.getCurrentInstance().getExternalContext()
                .getSession(true);
        miSession.setAttribute("usuario", claseUsuario);
        return true;
    } else
        return false;

}

From source file:Beans.ActorBeans.java

public void Ver() {
    Transaction t = null;/* ww  w . j  a  va 2s.  co m*/

    try {
        t = session.beginTransaction();
        String consulta = "from Actor";
        Query query = session.createQuery(consulta);
        Lista = query.list();

    } catch (HibernateException ex) {
        if (t != null) {
            t.rollback();
        }
    } finally {
        session.flush();
        session.close();
    }
}

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();

    long vreme = System.currentTimeMillis();

    Calendar datum = Calendar.getInstance();
    datum.setTimeInMillis(vreme);/*w w w  . ja  va2s  . co m*/

    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  v a2s.c o  m
        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 handleFileUpload(FileUploadEvent event) throws IOException {

    FacesMessage msg = new FacesMessage("Upload", event.getFile().getFileName() + " je postavljen na server.");
    FacesContext.getCurrentInstance().addMessage(null, msg);

    //        FacesContext context = FacesContext.getCurrentInstance();
    //        context.addMessage(null, new FacesMessage("Successful", "Your message: "));
    ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
    //        Random r = new Random();
    //        int broj = r.nextInt(100000);
    //        File result = new File(extContext.getRealPath("//resources//uploads//" + Integer.toString(broj) + event.getFile().getFileName()));      
    //        File result = new File("C:\\Users\\Marko\\Desktop\\proba\\" + event.getFile().getFileName());
    File result = new File(
            "C:\\Users\\Marko\\Desktop\\FAX\\Diplomski\\Diplomski\\src\\main\\webapp\\upload\\materijali\\"
                    + event.getFile().getFileName());
    //        File result = new File(extContext.getRealPath("..//..//web//resources//uploads//" + event.getFile().getFileName()));
    UploadedFile file = event.getFile();
    Materijal materijal = new Materijal();

    session = dbFactory.getFactory().openSession();
    Query q = session.createQuery("FROM Kurs");
    List<Kurs> kursevi = q.list();
    session.close();/*from  ww w  . j  av a  2  s .  c  o m*/
    Kurs k = new Kurs();
    if (kursevi.size() > 0) {
        k = kursevi.get(kursevi.size() - 1);
    }

    materijal.setPodatak(event.getFile().getFileName());
    materijal.setKurs(k.getIDKurs());
    materijal.setTip("predavanja");

    try {

        FileOutputStream fos = new FileOutputStream(result);
        InputStream is = file.getInputstream();
        int BUFFER_SIZE = 8192;
        byte[] buffer = new byte[BUFFER_SIZE];
        int a;
        while (true) {
            a = is.read(buffer);
            if (a < 0) {
                break;
            }
            fos.write(buffer, 0, a);
            fos.flush();
        }
        fos.close();
        is.close();

    } catch (IOException e) {
    }

    session = dbFactory.getFactory().openSession();
    session.beginTransaction();
    session.save(materijal);
    session.getTransaction().commit();
    session.close();
}

From source file:beans.Administrator.java

public void handleFileUploadVezbe(FileUploadEvent event) throws IOException {

    FacesMessage msg = new FacesMessage("Upload", event.getFile().getFileName() + " je postavljen na server.");
    FacesContext.getCurrentInstance().addMessage(null, msg);

    //        FacesContext context = FacesContext.getCurrentInstance();
    //        context.addMessage(null, new FacesMessage("Successful", "Your message: "));
    ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
    //        Random r = new Random();
    //        int broj = r.nextInt(100000);
    //        File result = new File(extContext.getRealPath("//resources//uploads//" + Integer.toString(broj) + event.getFile().getFileName()));      
    //        File result = new File("C:\\Users\\Marko\\Desktop\\proba\\" + event.getFile().getFileName());
    File result = new File(
            "C:\\Users\\Marko\\Desktop\\FAX\\Diplomski\\Diplomski\\src\\main\\webapp\\upload\\materijali\\"
                    + event.getFile().getFileName());
    //        File result = new File(extContext.getRealPath("..//..//web//resources//uploads//" + event.getFile().getFileName()));
    UploadedFile file = event.getFile();
    Materijal materijal = new Materijal();

    session = dbFactory.getFactory().openSession();
    Query q = session.createQuery("FROM Kurs");
    List<Kurs> kursevi = q.list();
    session.close();/*from   w w  w  . j  a  va2s . c  o m*/
    Kurs k = new Kurs();
    if (kursevi.size() > 0) {
        k = kursevi.get(kursevi.size() - 1);
    }

    materijal.setPodatak(event.getFile().getFileName());
    materijal.setKurs(k.getIDKurs());
    materijal.setTip("vezbe");

    try {

        FileOutputStream fos = new FileOutputStream(result);
        InputStream is = file.getInputstream();
        int BUFFER_SIZE = 8192;
        byte[] buffer = new byte[BUFFER_SIZE];
        int a;
        while (true) {
            a = is.read(buffer);
            if (a < 0) {
                break;
            }
            fos.write(buffer, 0, a);
            fos.flush();
        }
        fos.close();
        is.close();

    } catch (IOException e) {
    }

    session = dbFactory.getFactory().openSession();
    session.beginTransaction();
    session.save(materijal);
    session.getTransaction().commit();
    session.close();
}

From source file:beans.Administrator.java

public void inicijalizujPredavace() {

    session = dbFactory.getFactory().openSession();
    Query q = session.createQuery("FROM Korisnik WHERE tip=1");
    predavaciSvi = q.list();
    session.close();//from  w  w w .  j  a v a  2 s .  c o  m
    predavaci = new DualListModel<>(predavaciSvi, predavaciIzabrani);

    sourceStavke.add("obavestenja");
    sourceStavke.add("informacije");
    sourceStavke.add("materijali");
    sourceStavke.add("ispitni zadaci");
    sourceStavke.add("laboratorija/projektni zadaci");

    stavke = new DualListModel<>(sourceStavke, targetStavke);

}

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   ww w .j  a  v  a  2 s.  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 .  j a  v a 2 s  . co m
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();
    }
}