Example usage for org.hibernate Session get

List of usage examples for org.hibernate Session get

Introduction

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

Prototype

Object get(String entityName, Serializable id);

Source Link

Document

Return the persistent instance of the given named entity with the given identifier, or null if there is no such persistent instance.

Usage

From source file:Ciclo.editaCiclo.java

private void Selecciona2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Selecciona2ActionPerformed
    b_cancelar.requestFocus();//from ww  w .  j ava 2s .c o m
    h = new Herramientas(usr, 0);
    h.session(sessionPrograma);
    Session session = HibernateUtil.getSessionFactory().openSession();
    usr = (Usuario) session.get(Usuario.class, usr.getIdUsuario());
    if (usr.getEditarCiclo() == true) {
        altaCiclo obj = new altaCiclo(new javax.swing.JFrame(), true, usr, sessionPrograma);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        obj.setLocation((d.width / 2) - (obj.getWidth() / 2), (d.height / 2) - (obj.getHeight() / 2));
        obj.setVisible(true);
        obj.t_ciclo.requestFocus();
        borra_cajas();
        cajas(false, false, false, false);
        buscaDato();
    } else
        JOptionPane.showMessageDialog(null, "Acceso denegado!");
    if (session != null)
        if (session.isOpen())
            session.close();
}

From source file:Ciclo.editaCiclo.java

private void Eliminar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Eliminar1ActionPerformed
    Selecciona2.requestFocus();/*from www  . j  a va 2 s.c o m*/
    h = new Herramientas(usr, 0);
    h.session(sessionPrograma);
    Session session = HibernateUtil.getSessionFactory().openSession();
    usr = (Usuario) session.get(Usuario.class, usr.getIdUsuario());
    /*if(usr.getEditaCiclo()==true)
    {*/
    if (this.t_datos.getSelectedRow() >= 0) {
        DefaultTableModel model = (DefaultTableModel) t_datos.getModel();
        int a = t_datos.getSelectedRow();
        int opt = JOptionPane.showConfirmDialog(this, "Los datos capturados se eliminarn!");
        if (JOptionPane.YES_OPTION == opt) {
            boolean respuesta = eliminar(t_datos.getValueAt(t_datos.getSelectedRow(), 0).toString());
            if (respuesta == true) {
                JOptionPane.showMessageDialog(null, "El ciclo ha sido eliminado");
                model.removeRow(a);
                borra_cajas();
                cajas(false, false, false, false);
                buscaDato();
            }
        }
    } else
        cajas(false, false, false, false);
    /*}
    else
    JOptionPane.showMessageDialog(null, "Acceso denegado");*/
    if (session != null)
        if (session.isOpen())
            session.close();
}

From source file:Ciclo.editaCiclo.java

private void Selecciona1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Selecciona1ActionPerformed
    Eliminar1.requestFocus();/*from  ww w.  j  ava  2 s .  c  o m*/
    h = new Herramientas(usr, 0);
    h.session(sessionPrograma);
    Session session = HibernateUtil.getSessionFactory().openSession();
    usr = (Usuario) session.get(Usuario.class, usr.getIdUsuario());
    if (usr.getEditarCiclo() == true) {
        if (t_datos.getSelectedRow() >= 0) {
            this.borra_cajas();
            t_ciclo.setText(t_datos.getValueAt(t_datos.getSelectedRow(), 0).toString());
            ic = t_datos.getValueAt(t_datos.getSelectedRow(), 0).toString();
            t_descripcion.setText(t_datos.getValueAt(t_datos.getSelectedRow(), 1).toString());
            this.cajas(true, true, true, true);
        } else
            JOptionPane.showMessageDialog(null, "No hay un ciclo seleccionado!");
    } else
        JOptionPane.showMessageDialog(null, "Acceso denegado!");
    if (session != null)
        if (session.isOpen())
            session.close();
}

From source file:Ciclo.editaCiclo.java

private void b_guardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_guardarActionPerformed
    h = new Herramientas(usr, 0);
    h.session(sessionPrograma);/* ww  w .  ja  v  a2  s  .c o m*/
    bt_actualiza1.requestFocus();
    Session session = HibernateUtil.getSessionFactory().openSession();
    usr = (Usuario) session.get(Usuario.class, usr.getIdUsuario());
    if (usr.getEditarCiclo() == true) {
        if (t_ciclo.getText().compareTo("") != 0) {
            boolean respuesta = modifica();
            if (respuesta == true) {
                this.borra_cajas();
                cajas(false, false, false, false);
                buscaDato();
            }
        } else {
            JOptionPane.showMessageDialog(null, "Debe introducir el ciclo que desees modificar!");
            t_ciclo.requestFocus();
        }
    } else
        JOptionPane.showMessageDialog(null, "Acceso denegado!");
    if (session != null)
        if (session.isOpen())
            session.close();
}

From source file:Ciclo.editaCiclo.java

private boolean eliminar(String idCiclo) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {// ww  w  .j a v  a2s .  c om
        session.beginTransaction();
        actor = (Ciclo) session.get(Ciclo.class, Integer.parseInt(idCiclo));

        if (actor.getOrdens().isEmpty() == false) {
            session.getTransaction().rollback();
            JOptionPane.showMessageDialog(null, "El ciclo esta en uso en una orden no se puede eliminar!");
            return false;
        } else {
            session.delete(actor);
            session.getTransaction().commit();
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
        session.getTransaction().rollback();
        return false;
    } finally {
        if (session.isOpen())
            session.close();
    }
}

From source file:Ciclo.editaCiclo.java

private boolean modifica() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {//from  w  ww .  java 2s  .c  o m
        session.beginTransaction();
        Object resp = session.createQuery(
                "from Ciclo obj where obj.idCiclo='" + t_ciclo.getText() + "' and obj.idCiclo!='" + ic + "'")
                .uniqueResult();

        actor = (Ciclo) session.get(Ciclo.class,
                (Integer.parseInt(t_datos.getValueAt(t_datos.getSelectedRow(), 0).toString())));
        /*if(actor.getOrdens().isEmpty()==false) 
        {
        session.getTransaction().rollback();
        JOptionPane.showMessageDialog(null, "El Ciclo esta en uso en una orden no se puede modificar!");
        borra_cajas();
        return false;
        }
        else
        {*/
        if (resp == null) {
            Query q = session.createQuery("update Ciclo obj set obj.descripcion='" + t_descripcion.getText()
                    + "', obj.idCiclo='" + t_ciclo.getText() + "'where obj.idCiclo='" + ic + "'");
            q.executeUpdate();
            session.getTransaction().commit();
            JOptionPane.showMessageDialog(null, "Registro modificado");
            return true;
        } else {
            JOptionPane.showMessageDialog(null, "No se pueden guardar claves duplicadas");
            return false;
        }
        //}
    } catch (HibernateException he) {
        he.printStackTrace();
        System.out.println(he.hashCode());
        session.getTransaction().rollback();
        return false;
    } finally {
        if (session.isOpen())
            session.close();
    }
}

From source file:cimitero.rest.BodyRESTService.java

@GET
@Path("{id}")
public ResponseDto getBodyById(@PathParam("id") Integer id) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//  w w  w . j a  va 2  s  .c  om
    TBody result = (TBody) session.get(TBody.class, id);
    ResponseDto response = new ResponseDto(true);
    if (result == null) {
        response.setOk(false);
        response.addError(1, "body with id " + id + " not found");
    } else {
        List<TBody> results = new ArrayList<TBody>();
        results.add(result);
        response.setItems(new ItemWrapper(results));
    }
    session.getTransaction().commit();
    return response;
}

From source file:cimitero.rest.BodyRESTService.java

@POST
public ResponseDto updateBody(BodyDto bodyDto) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//ww w.j av  a2  s  .  c o m
    TBody tmpBody = new TBody(bodyDto.getPersonId(), bodyDto.getTitle(), bodyDto.getFirstName(),
            bodyDto.getLastName(), bodyDto.getOtherFirstNames(), bodyDto.getGebDatum(), bodyDto.getAddress(),
            bodyDto.getTelephoneNumber(), bodyDto.getDateOfDeath());
    TTomb tmpTomb = (TTomb) session.get(TTomb.class, bodyDto.getTombId());
    tmpBody.setTomb(tmpTomb);
    if (bodyDto.getPersonId() == -1)
        tmpBody.setPersonId(null);
    session.saveOrUpdate(tmpBody);
    ResponseDto response = new ResponseDto(true);
    session.getTransaction().commit();
    return response;
}

From source file:cimitero.rest.BodyRESTService.java

@DELETE
@Path("{id}")
public ResponseDto removeCoordinate(@PathParam("id") Integer id) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//w ww  .j av  a  2  s .co m
    TBody result = (TBody) session.get(TBody.class, id);
    session.delete(result);
    ResponseDto response = new ResponseDto(true);
    session.getTransaction().commit();
    return response;
}

From source file:cimitero.rest.CemetryGroundRESTService.java

@GET
@Path("{id}")
public ResponseDto getCemetryGroundById(@PathParam("id") Integer id) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/* ww  w  .j  av a 2 s  .c o  m*/
    TCemetryGround result = (TCemetryGround) session.get(TCemetryGround.class, id);
    ResponseDto response = new ResponseDto(true);
    if (result == null) {
        response.setOk(false);
        response.addError(1, "cemetry ground with id " + id + " not found");
    } else {
        List<TCemetryGround> results = new ArrayList<TCemetryGround>();
        results.add(result);
        response.setItems(new ItemWrapper(results));
    }
    session.getTransaction().commit();
    return response;
}