Example usage for org.hibernate Session createQuery

List of usage examples for org.hibernate Session createQuery

Introduction

In this page you can find the example usage for org.hibernate Session createQuery.

Prototype

@Override
    org.hibernate.query.Query createQuery(CriteriaDelete deleteQuery);

Source Link

Usage

From source file:Beans.Reporte_Proyectos.java

public ArrayList Traer_proyectos(String x, int condi) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction t = session.beginTransaction();
    ArrayList<Proyectos> Lista_Proyectos = new ArrayList();
    try {//from w  w w. j a  va2s  .  co  m
        if (condi == 1) {
            Lista_Proyectos = (ArrayList) session.createQuery("select distinct P from Proyectos P INNER JOIN "
                    + "P.usuarioProyectos UP INNER JOIN UP.usuarioByEstudiante U "
                    + "INNER JOIN P.estadoProyectos EP INNER JOIN EP.estados E INNER JOIN P.tipoProyecto T "
                    + "where U.pegeId=" + x + " and E.codigoEstados=" + cod_estado + " and T.codTipo=" + tipo)
                    .list();
        } else if (condi == 2) {
            if (tipo == 1) {
                Lista_Proyectos = (ArrayList) session
                        .createQuery("select distinct P from Proyectos P INNER JOIN "
                                + "P.usuarioProyectos UP INNER JOIN UP.usuarioByEstudiante U "
                                + "INNER JOIN P.estadoProyectos EP INNER JOIN EP.estados E INNER JOIN P.tipoProyecto T "
                                + "where U.pegeId=" + x + " and E.codigoEstados=" + 6 + " and T.codTipo="
                                + tipo)
                        .list();
            } else if (tipo == 2) {
                Lista_Proyectos = (ArrayList) session
                        .createQuery("select distinct P from Proyectos P INNER JOIN "
                                + "P.usuarioProyectos UP INNER JOIN UP.usuarioByEstudiante U "
                                + "INNER JOIN P.tipoProyecto T " + "where U.pegeId=" + x + " and T.codTipo="
                                + tipo)
                        .list();
            }

        }
        t.commit();

    } catch (Exception ex) {

    }
    return Lista_Proyectos;
}

From source file:Beans.ValidarProfesor.java

public DispoUsuario comprarProfesor(String pege) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction t = session.beginTransaction();
    System.out.println("entro a comprobar pege " + pege);
    DispoUsuario temp = new DispoUsuario();
    try {/*from  w w  w. java 2  s.c o  m*/
        temp = (DispoUsuario) session.createQuery("select Dis from Disponibilidad"
                + "  D INNER JOIN D.dispoUsuarios Dis " + " INNER JOIN Dis.usuarioByProfesor U where "
                + "  U.pegeId=" + pege + " and D.estado='A'").uniqueResult();
        //            System.out.println("temp " + temp.toString());
        t.commit();
    } catch (Exception ex) {
        System.out.println("Error comprobar profe" + ex.toString());
    }
    return temp;
}

From source file:bikerent.AddCustomer.java

public static void main(String[] args) {
    // Configure the session factory
    configureSessionFactory();//  w w w.j ava2s .  c  o  m

    NewCustomer custframe = new NewCustomer();
    custframe.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            if (custframe.getOK()) {

                System.out.println("Firstname: " + custframe.getFirstname());
                System.out.println("Lastname: " + custframe.getLastname());
                System.out.println("Dob: " + custframe.getDob());
                System.out.println("Mail: " + custframe.getMail());
                System.out.println("Password: " + custframe.getPW());

                Address addr = new Address(custframe.getStreet(), custframe.getHousenr(), custframe.getZip(),
                        custframe.getCity(), custframe.getCountry());
                this.saveCustomer_(custframe.getFirstname(), custframe.getLastname(), custframe.getDob(),
                        custframe.getMail(), custframe.getPW(), addr);
            }

        }

        public Boolean saveCustomer_(String fname, String lname, String DOB, String Mail, String PW,
                Address addr) {

            SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
            java.util.Date date = null;
            try {
                date = format.parse(DOB);
            } catch (ParseException ex) {
                Logger.getLogger(AddCustomer.class.getName()).log(Level.SEVERE, null, ex);
            }
            java.sql.Date sqlDate = new java.sql.Date(date.getTime());
            Customer cust;
            cust = new Customer(fname, lname, sqlDate, Mail, PW, addr);

            Session session = null;
            Transaction tx = null;

            try {
                session = sessionFactory.openSession();
                tx = session.beginTransaction();
                // Saving to the database
                session.save(cust);
                // Committing the change in the database.
                session.flush();
                tx.commit();

                // Fetching saved data
                List<Customer> custList = session.createQuery("from Customer").list();

                System.out.println("customer:");
                for (Customer c : custList) {
                    System.out.println("Name: " + c.getName());
                }
                System.out.println("addresses:");
                // Fetching saved data
                List<Address> addrList = session.createQuery("from Address").list();

                for (Address a : addrList) {
                    System.out.println("Name: " + a.getStreet());
                }

            } catch (Exception ex) {
                ex.printStackTrace();

                // Rolling back the changes to make the data consistent in case of any failure 
                // in between multiple database write operations.
                tx.rollback();
            } finally {
                if (session != null) {
                    session.close();
                }
            }
            return true;
        }
    });
}

From source file:bikerent.AddDeleteEntity.java

@Test
public void testAUDEntity() {

    // #############################################
    // ################## A D D ####################
    // #############################################

    System.out.println("TEST INSERT: ");
    System.out.println("3 Customer (3 Personen) hinzufgen ");
    System.out.println("2 Female-, 1 Male-, 1 Child-Bike (4 Bikes) hinzufgen ");
    System.out.println("3 Helmets, 3 Chainlocks, 2 Trailer (8 Accessoires) hinzufgen ");

    Session session = sm.getSession();
    addCustomer(session);/*from  ww  w  .ja va  2 s  .c o m*/
    addBikes(session);
    addAccessiores(session);
    // Committing the change in the database.
    session.flush();
    Transaction tx = session.beginTransaction();
    tx.commit();
    System.out.println(" -------------------------------------------- ");
    System.out.println("Anzahl Bikes: " + countBikes(session));
    System.out.println("Anzahl Person: " + countPerson(session));
    System.out.println("Anzahl Accessoires: " + countAccessoires(session));
    System.out.println("Anzahl Items: " + countItem(session));
    System.out.println(" -------------------------------------------- ");
    assertEquals("Count of bikes must be 4: ", 4, countBikes(session));
    assertEquals("Count of person must be 3: ", 3, countPerson(session));
    assertEquals("Count of accessoires must be 8: ", 8, countAccessoires(session));
    assertEquals("Count of item must be 12: ", 12, countItem(session));

    System.out.println(printBikes(session));

    // #############################################
    // ############### U P D A T E #################
    // #############################################

    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println("TEST UPDATE: ");
    System.out.println("Person mit der ID 1 (Sheldon Cooper) auslesen, umbenennen und wieder auslesen");

    System.out.println(printPerson(session));

    custID = getFirstPersonID(session);
    Object object = session.load(Person.class, custID);
    Person p = (Person) object;
    System.out.println(" -------------------------------------------- ");
    System.out.println("Name: " + p.GetName());

    Transaction tx1 = session.beginTransaction();
    p.SetName("Howard", "Wolowitz");
    session.update(p);
    session.flush();
    tx1.commit();

    object = session.load(Person.class, custID);
    p = (Person) object;
    System.out.println("Neuer Name: " + p.GetName());
    System.out.println(" -------------------------------------------- ");

    System.out.println(printPerson(session));

    object = session.load(Customer.class, custID);
    Customer c = (Customer) object;
    System.out.println("Neuer Name des betroffenen CUSTOMERS (Subklasse): " + c.GetName());
    System.out.println(" -------------------------------------------- ");

    assertEquals("Count of person must be 3: ", 3, countPerson(session));

    // #############################################
    // ############### D E L E T E #################
    // #############################################

    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println("TEST DELETE: ");
    System.out.println("Alle Items lschen und prfen, ob noch Bikes existieren");

    System.out.println(" -------------------------------------------- ");
    System.out.println("Momentane Anzahl an Items: " + countItem(session));
    System.out.println("Momentane Anzahl an Bikes: " + countBikes(session));
    List<Item> iList = session.createQuery("from Item").list();
    Transaction tx2 = session.beginTransaction();
    for (Item i : iList) {
        session.delete(i);
    }
    session.flush();
    tx2.commit();

    System.out.println("Neue Anzahl an Items: " + countBikes(session));
    System.out.println("Neue Anzahl an Bikes: " + countBikes(session));
    System.out.println(" -------------------------------------------- ");

    assertEquals("Count of bikes must be 0: ", 0, countBikes(session));

}

From source file:bikerent.AddDeleteEntity.java

private static int countBikes(Session session) {
    List<Bike> bList = session.createQuery("from Bike").list();
    return bList.size();
}

From source file:bikerent.AddDeleteEntity.java

private static String printBikes(Session session) {
    List<Bike> bList = session.createQuery("from Bike").list();
    String res = "Bikes: \n";
    for (Bike b : bList) {
        res += b.printBike() + "\n";
    }//from   w w  w.  ja va2 s . co  m
    return res;
}

From source file:bikerent.AddDeleteEntity.java

private static int countPerson(Session session) {
    List<Person> pList = session.createQuery("from Person").list();
    return pList.size();
}

From source file:bikerent.AddDeleteEntity.java

private static String printPerson(Session session) {
    List<Person> pList = session.createQuery("from Person").list();
    String res = "Person: \n";
    for (Person p : pList) {
        res += p.printPerson() + "\n";
    }/*from ww w.j  a  v  a  2s .  com*/
    return res;
}

From source file:bikerent.AddDeleteEntity.java

private static Long getFirstPersonID(Session session) {
    List<Person> pList = session.createQuery("from Person").list();
    return pList.get(0).getID();
}

From source file:bikerent.AddDeleteEntity.java

private static int countAccessoires(Session session) {
    List<Accessoires> aList = session.createQuery("from Accessoires").list();
    return aList.size();
}