Example usage for org.hibernate Session flush

List of usage examples for org.hibernate Session flush

Introduction

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

Prototype

void flush() throws HibernateException;

Source Link

Document

Force this session to flush.

Usage

From source file:banco.UsuarioDAO.java

public boolean checkLogin(Usuario usuario) {

    List<Usuario> listUsuario = new ArrayList<Usuario>();
    Transaction trns = null;//ww w. j  a  v  a  2  s  .co  m
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        trns = session.beginTransaction();
        listUsuario = session.createQuery("from Usuario").list();

        for (Usuario key : listUsuario) {
            if (key.getSenha().equals(usuario.getSenha()) && key.getLogin().equals(usuario.getLogin())) {
                return true;
            }
        }

    } catch (RuntimeException e) {
        e.printStackTrace();
    } finally {
        session.flush();
        session.close();
    }
    return false;
}

From source file:bank.DAO.BillDAO.java

public void deleteEntity(Object o) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*from w  ww .  ja  v a  2 s .  c  o m*/
    session.delete(o);
    session.flush();
    session.getTransaction().commit();
}

From source file:bikerent.AddCustomer.java

public static void main(String[] args) {
    // Configure the session factory
    configureSessionFactory();// w  w  w.j a v a 2 s .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);//  w w  w .j  a v a  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

public void UpdateEntity() {

    //Selecting Student Records
    Session session = sm.getSession();
    custID = getFirstPersonID(session);// ww  w  . j  a v a  2s. co m
    Object object = session.load(Person.class, custID);
    Person p = (Person) object;
    System.out.println("Name: " + p.GetName());

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

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

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

From source file:bikerent.BikeRent.java

public static void main(String[] args) {
    // Configure the session factory
    configureSessionFactory();/*from  w w  w  . j ava 2  s  .  c om*/

    Session session = null;
    Transaction tx = null;

    try {
        session = sessionFactory.openSession();
        tx = session.beginTransaction();

        // Creating Contact entity that will be save to the sqlite database
        Address myAddress = new Address();

        // Saving to the database
        session.save(myAddress);

        // Committing the change in the database.
        session.flush();
        tx.commit();

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

        for (Address add : userList) {
            System.out.println("Id: " + add.getId());
        }

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

}

From source file:bikerent.Test1.java

@org.junit.Test
public void test() {
    configureSessionFactory();/*www  .  j  a v  a2s  .  com*/

    Session session = null;
    Transaction tx = null;

    session = sessionFactory.openSession();
    tx = session.beginTransaction();
    addCustomer(session);
    addBikes(session);
    addAccessiores(session);
    // Committing the change in the database.
    session.flush();
    tx.commit();
    System.out.println(" ############################### ");
    System.out.println(countBikes(session));
    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));
}

From source file:bo.gaceta.rcb.dao.impl.TbInCiiuImpl.java

public List<TbInCiiu> list() throws Exception {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();//ww  w. j a  v  a2  s  .  co m
    session.flush();
    Query query = session.createQuery("SELECT e FROM TbInCiiu e");
    List<TbInCiiu> entities = query.list();
    session.getTransaction().commit();
    session.close();
    return entities;
}

From source file:bo.gaceta.rcb.dao.impl.TbInCiiuImpl.java

public List<TbInCiiu> listFiltroClase(String txtClase) throws Exception {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();/*from   w ww  .jav a2  s  .  c om*/
    session.flush();
    Query query = session.createQuery("SELECT e FROM TbInCiiu e WHERE e.clase LIKE :_txtBusca")
            .setParameter("_txtBusca", "%" + txtClase + "%");
    List<TbInCiiu> entities = query.list();
    session.getTransaction().commit();
    session.close();
    return entities;
}

From source file:bo.gaceta.rcb.dao.impl.TbInCiiuImpl.java

public List<TbInCiiu> listSeccion() throws Exception {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();/*from w  w  w  . j  ava2 s .com*/
    session.flush();
    Query query = session.createQuery("SELECT e FROM TbInCiiu  e GROUP BY e.idSeccion");
    List<TbInCiiu> entities = query.list();
    session.getTransaction().commit();
    session.close();
    return entities;
}