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:aseguradora.VistaVentana.java

public void cargarVista() {
    try {/*from ww  w  .  ja  v a 2  s  .c  om*/
        String[] columnNames = { "Cod. Poliza", "Datos Poliza", "Num", "Nombre Asegurado", "FN" };
        DefaultTableModel model = new DefaultTableModel(columnNames, 0);

        Session session = sesion.openSession();

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

        while (iter.hasNext()) {
            pa = (PolizasAsegurados) iter.next();
            Hibernate.initialize(pa.getId());
            Vector row = new Vector();
            row.add(pa.getId().getCodP());
            row.add(pa.getId().getDatosP());
            row.add(pa.getId().getNum());
            row.add(pa.getId().getNa());
            row.add(formatDate(pa.getId().getFn().toString()));
            model.addRow(row);
        }

        tablaVista.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.VistaVentana.java

private void buscarAsegurado(String nombre) {
    Session session = sesion.openSession();
    PolizasAsegurados pa;//from  w  ww.  java2 s  . c  om
    Query cons = session.createQuery("from pojo.PolizasAsegurados as pa " + "where upper(pa.id.na) LIKE ?");
    cons.setString(0, "%" + nombre.toUpperCase() + "%");
    List<PolizasAsegurados> lista = cons.list();
    DefaultTableModel model = (DefaultTableModel) tablaVista.getModel();
    if (!lista.isEmpty()) {
        model.getDataVector().removeAllElements();
        model.fireTableDataChanged();
        Iterator<PolizasAsegurados> iter = lista.iterator();
        while (iter.hasNext()) {
            pa = (PolizasAsegurados) iter.next();
            Hibernate.initialize(pa.getId());
            Vector row = new Vector();
            row.add(pa.getId().getCodP());
            row.add(pa.getId().getDatosP());
            row.add(pa.getId().getNum());
            row.add(pa.getId().getNa());
            row.add(formatDate(pa.getId().getFn().toString()));
            model.addRow(row);
        }
        tablaVista.setModel(model);
        session.close();
    } else {
        model.getDataVector().removeAllElements();
        model.fireTableDataChanged();
        JOptionPane.showMessageDialog(null, "No hay resultados para su bsqueda", "Informacin",
                JOptionPane.ERROR_MESSAGE);
        cargarVista();
    }

}

From source file:aseguradora.VistaVentana.java

private void actualizarVista() {
    try {/*from w w w . j a va2 s.c om*/
        Session session = sesion.openSession();
        Transaction tx = session.beginTransaction();

        String hql = "update PolizasAsegurados pa set " + "pa.id.na = :na,pa.id.fn = :fn "
                + "where pa.id.codP = :codp " + "and pa.id.num = :num";
        Date fechaAsegurado = (Date) jsFechaAsegurado.getValue();
        int query = session.createQuery(hql).setString("na", etNomAsegurado.getText())
                .setDate("fn", fechaAsegurado).setInteger("codp", Integer.parseInt(etCodPoliza.getText()))
                .setInteger("num", Integer.parseInt(etNumAsegurado.getText())).executeUpdate();

        tx.commit();
        session.close();
        cargarVista();
    } catch (JDBCException e) {
        e.printStackTrace();
        if (e.getErrorCode() == CLAVE_PRIMARIA_DUPLICADA) {
            JOptionPane.showMessageDialog(rootPane, "Existe una pliza con ese identificador (ID)");
        }
        if (e.getErrorCode() == CLAVE_AJENA_NO_ENCONTRADA) {
            JOptionPane.showMessageDialog(rootPane, "No existe una pliza con ese identificador (ID)");
        }
        if (e.getErrorCode() == VALOR_DEMASIADO_LARGO) {
            JOptionPane.showMessageDialog(rootPane, "Has introducido un valor demasiado largo");
        }
        ;
        if (e.getErrorCode() == ERROR_EDITAR_CLAVE_AJENA) {
            JOptionPane.showMessageDialog(rootPane, "No puedes editar el id de este asegurado");
        }
        ;
    }
}

From source file:aseguradora.VistaVentana.java

private void eliminarAsegurado() {
    try {/* www .  ja  v  a2 s  .c o  m*/
        Session session = sesion.openSession();
        Transaction tx = session.beginTransaction();
        PolizasAsegurados pa;

        Query q = session
                .createQuery("from PolizasAsegurados as pa " + "where pa.id.codP = ? " + "and pa.id.num = ?");

        q.setInteger(0, Integer.parseInt(etCodPoliza.getText()));
        q.setInteger(1, Integer.parseInt(etNumAsegurado.getText()));

        pa = (PolizasAsegurados) q.uniqueResult();

        session.delete(pa);
        tx.commit();
        session.close();
        cargarVista();
    } catch (JDBCException e) {
        e.printStackTrace();
        if (e.getErrorCode() == ERROR_EDITAR_CLAVE_AJENA) {
            JOptionPane.showMessageDialog(rootPane,
                    "No puedes borrar este asegurado, restriccin clave primeria/ajena");
        }
    }
}

From source file:at.molindo.esi4j.module.hibernate.scrolling.SimpleQueryProvider.java

License:Apache License

@Override
public final Query createQuery(Class<?> type, Session session) {
    return _hql == null ? null : session.createQuery(_hql);
}

From source file:at.thinkingco2.databasemanager.CarAccess.java

/**
 * This function returns a CarBean specified by its LicenseNumber
 * //from  w w  w  . j  a  va 2s  .c  o m
 * @param licenseNumber
 *            the LicenseNumber of the searched Car
 * @return the CarBean
 */
public CarBean getCarByLicenseNumber(String licenseNumber) {
    Session session = getSession();
    CarBean bean = null;
    Query q = null;

    q = session.createQuery("FROM CarBean WHERE licenseNumber = :licenseNumber");
    q.setParameter("licenseNumber", licenseNumber);

    bean = (CarBean) q.uniqueResult();
    session.close();

    return bean;
}

From source file:at.thinkingco2.databasemanager.JourneyAccess.java

/**
 * This Function returns a list of JourneyBeans, which have no current drive
 * (JounreyBean which searches a lift).//w w  w  .j  av a  2 s .c o m
 * 
 * @return a List of JounreyBean
 */
@SuppressWarnings("unchecked")
public List<JourneyBean> getJourneysWithoutDriver() {
    Session session = getSession();
    List<JourneyBean> list = null;
    Query q = null;

    q = session.createQuery("FROM JourneyBean WHERE licenseNumber IS NULL");

    list = q.list();
    session.close();

    return list;
}

From source file:at.thinkingco2.databasemanager.JourneyAccess.java

/**
 * This Function returns a list of JourneyBeans, which have a current driver
 * /*  w w w.  j  a v  a 2s . co  m*/
 * @return a List of JounreyBeans
 */
@SuppressWarnings("unchecked")
public List<JourneyBean> getJourneysWithDriver() {
    Session session = getSession();
    List<JourneyBean> list = null;
    Query q = null;

    q = session.createQuery("FROM JourneyBean WHERE licenseNumber NOT NULL");

    list = q.list();
    session.close();

    return list;
}

From source file:at.thinkingco2.databasemanager.JourneyAccess.java

/**
 * This function returns the Number of used Seats in a given Route
 * //from w  w w. j av  a 2s.co m
 * @param routeId
 *            The routeId
 * @return Number of used Seats
 */
@SuppressWarnings("unchecked")
public Integer getUsedSeats(Integer routeId) {
    Session session = getSession();
    List<JourneyBean> journeyList = null;
    Integer seats = 0;
    Query q = null;

    q = session.createQuery("FROM JourneyBean WHERE routeId = :routeId");
    q.setParameter("routeId", routeId);

    journeyList = q.list();

    for (JourneyBean journey : journeyList) {
        seats += journey.getChildren().size();
    }

    session.close();

    return seats + 1; // +1 = + parent
}

From source file:at.thinkingco2.databasemanager.JourneyAccess.java

@SuppressWarnings("unchecked")
public List<JourneyBean> getJourneysWithDriver(Set<GroupBean> groups) {
    Session session = getSession();

    Set<GroupBean> groupSet = null;
    Iterator<GroupBean> it = null;
    ArrayList<JourneyBean> journeyList = new ArrayList<JourneyBean>();

    List<JourneyBean> temp = session.createQuery("FROM JourneyBean WHERE licenseNumber IS NOT NULL").list();

    for (JourneyBean journey : temp) {
        groupSet = journey.getGroups();//from   w ww . j a v  a 2 s .  co  m
        it = groups.iterator();
        while (it.hasNext()) {
            if (groupSet.contains(it.next())) {
                journeyList.add(journey);
            }
        }
    }

    session.close();

    return journeyList;
}