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:appHibernateSebastianLeonte.Main.java

public static void consulta() {
    SessionFactory session = SessionFactoryUtil.getSessionFactory();
    Session s = session.openSession();
    Transaction transaction = s.beginTransaction();
    System.out.println("Modificando");
    Vuelos vuelo = new Vuelos();
    Query q = s.createQuery("from Vuelos where Destino='BARCELONA' or Destino='MADRID'");
    List<Vuelos> listaVuelo = q.list();
    Iterator<Vuelos> iter = listaVuelo.iterator();
    while (iter.hasNext()) {
        vuelo = (Vuelos) iter.next();/* w  ww .j a va 2 s . c o m*/
        System.out.println("PROCEDENCIA: " + vuelo.getProcedencia() + "\t");
        System.out.println("DESTINO: " + vuelo.getDestino() + "\t");
        System.out.println("HORA DE SALIDA: " + vuelo.getHoraSalida());
    }
    s.close();
    session.close();
}

From source file:applicationController.Handlers.GetAllChatsHandler.java

@Override
public void handleIt(HashMap inMap, JSONOutputStream outToClient) {
    Session session = HibernateUtilSingleton.getSessionFactory().getCurrentSession();
    Transaction transaction = session.beginTransaction();
    Query query = session.createQuery("FROM Meeting WHERE meeting_id = :meeting ");
    query.setParameter("meeting", this.commBean.convertLong(((HashMap) inMap.get("data")).get("meeting")));
    Meeting meeting = (Meeting) query.uniqueResult();
    String returnMsg = "";
    if (meeting != null) {
        for (Message msg : meeting.getMessages()) {
            returnMsg += msg.getUsername() + ": " + msg.getMsg() + "\n";
        }/*from  ww w  . j av  a2  s .co  m*/
    } else {
        Meeting newMeeting = new Meeting();
        newMeeting.setMeeting_id(this.commBean.convertLong(((HashMap) inMap.get("data")).get("meeting")));
        newMeeting.setName("not named yet");
        session.save(newMeeting);
    }

    session.getTransaction().commit();

    this.commBean.setCommand("SENDCHAT");
    HashMap data = new HashMap();
    data.put("msg", returnMsg);
    this.commBean.setData(data);

    try {
        outToClient.writeObject(this.commBean);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:applicationController.Handlers.SendChatHandler.java

@Override
public void handleIt(HashMap inMap, JSONOutputStream outToClient) {
    Session session = HibernateUtilSingleton.getSessionFactory().getCurrentSession();
    Transaction transaction = session.beginTransaction();
    Query query = session.createQuery("FROM Meeting WHERE meeting_id = :meeting ");
    query.setParameter("meeting", this.commBean.convertLong(((HashMap) inMap.get("data")).get("meeting")));
    Meeting meeting = (Meeting) query.uniqueResult();
    Message msg = new Message();
    msg.setMeeting(meeting);/*w ww.  java2  s.c o m*/
    msg.setUsername(((HashMap) inMap.get("data")).get("username").toString());
    msg.setMsg(((HashMap) inMap.get("data")).get("msg").toString());
    session.save(msg);
    session.getTransaction().commit();

    this.commBean.setCommand("SENDCHAT");
    HashMap data = new HashMap();
    data.put("msg", ((HashMap) inMap.get("data")).get("msg").toString());
    data.put("username", ((HashMap) inMap.get("data")).get("username").toString());
    this.commBean.setData(data);

    try {
        outToClient.writeObject(this.commBean);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:appointment.businessobject.ManageCustomer.java

public void listCustomers() {
    Session session = factory.openSession();
    Transaction tx = null;/*w w  w . ja  va 2  s  .  c  o  m*/
    try {
        tx = session.beginTransaction();
        List employees = session.createQuery("FROM Customer").list();
        for (Iterator iterator = employees.iterator(); iterator.hasNext();) {
            Customer employee = (Customer) iterator.next();
            System.out.print("First Name: " + employee.getFirstName());
            System.out.print("  Last Name: " + employee.getLastName() + "\n");

        }
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
    } finally {
        session.close();
    }
}

From source file:appointment.businessobject.ManageCustomer.java

public ArrayList<Customer> getAllCustomers() {
    Session session = factory.openSession();
    Transaction tx = null;/*from   ww  w . j av a 2s.com*/
    ArrayList<Customer> cust = new ArrayList();
    try {
        tx = session.beginTransaction();
        List customers = session.createQuery("FROM Customer").list();
        for (Iterator iterator = customers.iterator(); iterator.hasNext();) {
            Customer customer = (Customer) iterator.next();
            cust.add(customer);
        }
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
    } finally {
        session.close();
    }
    return cust;
    //return null;
}

From source file:ar.com.hibernate.dao.Dao.java

public <T> List<T> obtenerTodos(Class<T> clazz) {
    Session s = SessionManager.getSessionFactory().openSession();
    Query q = s.createQuery("from " + clazz.getName());
    List<T> entities = q.list();
    s.close();/*from w  ww .  j a  v a2  s . c om*/
    return entities;
}

From source file:aseguradora.HospitalVentana.java

public void cargarHospital() {
    try {//from w ww  . j  a  v  a  2 s .c o  m
        String[] columnNames = new String[3];
        DefaultTableModel model = new DefaultTableModel(columnNames, 0);

        Session session = sesion.openSession();

        Hospital hos = new Hospital();
        Query cons = session.createQuery("from pojo.Hospital");
        List<Hospital> lista = cons.list();
        Iterator<Hospital> iter = lista.iterator();

        while (iter.hasNext()) {
            hos = (Hospital) iter.next();
            Hibernate.initialize(hos.getCodH());
            Vector row = new Vector();
            row.add(hos.getCodH());
            row.add(hos.getNH());
            row.add(hos.getNumC());
            model.addRow(row);
        }

        tablaHospital.setModel(model);
        session.close();
    } catch (HibernateException e) {
        e.printStackTrace();
        if (e.getMessage().contains("" + TABLA_NO_ENCONTRADA)) {
            JOptionPane.showMessageDialog(rootPane,
                    "Tabla no encontrada. Pongase en contacto con el administrador.");
        }
    }
}

From source file:aseguradora.HospitalVentana.java

private void buscarHospital(String nombre) {

    Session session = sesion.openSession();
    Hospital hos;/*from w  w w. ja  v a  2s. com*/
    Query cons = session.createQuery("from pojo.Hospital as hos " + "where upper(hos.NH) LIKE ?");
    cons.setString(0, "%" + nombre.toUpperCase() + "%");
    List<Hospital> lista = cons.list();
    DefaultTableModel model = (DefaultTableModel) tablaHospital.getModel();
    if (!lista.isEmpty()) {
        model.getDataVector().removeAllElements();
        model.fireTableDataChanged();
        Iterator<Hospital> iter = lista.iterator();
        while (iter.hasNext()) {
            hos = (Hospital) iter.next();
            Hibernate.initialize(hos.getCodH());
            Vector row = new Vector();
            row.add(hos.getCodH());
            row.add(hos.getNH());
            row.add(hos.getNumC());
            model.addRow(row);
        }
        tablaHospital.setModel(model);
        session.close();
    } else {
        model.getDataVector().removeAllElements();
        model.fireTableDataChanged();
        JOptionPane.showMessageDialog(null, "No hay resultados para su bsqueda", "Informacin",
                JOptionPane.ERROR_MESSAGE);
        cargarHospital();
    }

}

From source file:aseguradora.MedicoVentana.java

public void cargarMedico() {
    try {/*  w  ww .jav  a  2  s.c o  m*/
        String[] columnNames = new String[3];
        DefaultTableModel model = new DefaultTableModel(columnNames, 0);

        Session session = sesion.openSession();

        Medico med = new Medico();
        Query cons = session.createQuery("from pojo.Medico");
        List<Medico> lista = cons.list();
        Iterator<Medico> iter = lista.iterator();

        while (iter.hasNext()) {
            med = (Medico) iter.next();
            Hibernate.initialize(med.getHospital());
            Vector row = new Vector();
            row.add(med.getCodM());
            row.add(med.getNM());
            row.add(med.getHospital().getCodH());
            model.addRow(row);
        }

        tablaMedico.setModel(model);
        session.close();

    } catch (JDBCException e) {
        e.printStackTrace();
        if (e.getErrorCode() == TABLA_NO_ENCONTRADA) {
            JOptionPane.showMessageDialog(rootPane,
                    "Tabla no encontrada. Pongase en contacto con el administrador.");
        }
    }
}

From source file:aseguradora.MedicoVentana.java

private void buscarMedico(String nombre) {

    Session session = sesion.openSession();
    Medico me;//from w ww  . j  av a 2  s  . co  m
    Query cons = session.createQuery("from pojo.Medico as me " + "where upper(me.NM) LIKE ?");
    cons.setString(0, "%" + nombre.toUpperCase() + "%");
    List<Medico> lista = cons.list();
    DefaultTableModel model = (DefaultTableModel) tablaMedico.getModel();
    if (!lista.isEmpty()) {
        model.getDataVector().removeAllElements();
        model.fireTableDataChanged();
        Iterator<Medico> iter = lista.iterator();
        while (iter.hasNext()) {
            me = (Medico) iter.next();
            Hibernate.initialize(me.getHospital());
            Vector row = new Vector();
            row.add(me.getCodM());
            row.add(me.getHospital().getCodH());
            row.add(me.getNM());
            model.addRow(row);
        }
        tablaMedico.setModel(model);
        session.close();
    } else {
        model.getDataVector().removeAllElements();
        model.fireTableDataChanged();
        JOptionPane.showMessageDialog(null, "No hay resultados para su bsqueda", "Informacin",
                JOptionPane.ERROR_MESSAGE);
        cargarMedico();
    }

}