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:Clientes.editaCliente.java

private boolean modifica() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {//from w  w  w .j  ava 2  s  .com
        session.beginTransaction();
        Object resp = session.createQuery("from Clientes obj where obj.nombre='" + nombre.getText()
                + "' and obj.idClientes!=" + IdClientes.getText()).uniqueResult();
        if (resp == null) {
            Clientes objeto = (Clientes) session.get(Clientes.class, Integer.parseInt(IdClientes.getText()));
            objeto.setNombre(nombre.getText());
            objeto.setDireccion(Direccion.getText());
            objeto.setColonia(Colonia.getText());
            if (Cp.getText().compareTo("") == 0)
                objeto.setCp(null);
            else
                objeto.setCp(Integer.parseInt(Cp.getText()));
            objeto.setRfc(Rfc.getText().trim());
            objeto.setPoblacion(Poblacion.getText().trim());
            objeto.setEstado(Estado.getSelectedItem().toString().trim());
            objeto.setTelefono(Telefono.getText().trim());
            objeto.setEmail(Email.getText().trim());
            objeto.setContacto(contacto.getText().trim());
            objeto.setNextel(nextel.getText().trim());
            objeto.setMunicipio(municipio.getText().trim());
            objeto.setNumeroExterior(numero.getText().trim());
            objeto.setReceptor(t_receptor.getText());
            objeto.setEmailReceptor(t_email_receptor.getText());
            session.update(objeto);
            session.getTransaction().commit();
            cajas(false);
            return true;
        } else {
            JOptionPane.showMessageDialog(null, "No se pueden guardar nombres duplicados!");
            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:Clientes.editaCliente.java

private boolean eliminar(String idCliente) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {//from w w  w . ja va 2 s.c  o  m
        session.beginTransaction();
        actor = (Clientes) session.get(Clientes.class, Integer.parseInt(idCliente));
        if (actor.getOrdens().isEmpty() == false) {
            session.getTransaction().rollback();
            JOptionPane.showMessageDialog(null, "El cliente 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:cn.dao.MedicineInfoDao.java

public MedicineInfo queryById(MedicineInfo info) {
    Session session = this.getSessionFactory().getCurrentSession();
    return session.get(MedicineInfo.class, info.getId());
}

From source file:cn.dao.MedicinePurchaseDao.java

public MedicinePurchase queryById(MedicinePurchase info) {
    Session session = this.getSessionFactory().getCurrentSession();
    return session.get(MedicinePurchase.class, info.getId());
}

From source file:cn.dao.MedicineSalesDao.java

public MedicineSales queryById(MedicineSales info) {
    Session session = this.getSessionFactory().getCurrentSession();
    return session.get(MedicineSales.class, info.getId());
}

From source file:cn.newtouch.model.StudentTest.java

License:Open Source License

@Test
public void testUpdate() {

    Session session = HibernateUtil.getSession();
    session.beginTransaction();/*  w ww  . j  a  v  a  2 s .  c  o  m*/
    try {
        Student s = (Student) session.get(Student.class, 1L);
        s.setName("zhangsan21231");
        session.getTransaction().commit();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:cn.newtouch.model.TeacherTest.java

License:Open Source License

@Test
public void testGet() {

    Session session = HibernateUtil.getSession();
    session.beginTransaction();/* w  ww. j  ava2 s.  c o m*/
    Teacher t = (Teacher) session.get(Teacher.class, 55);

    session.getTransaction().commit();
    System.out.println(t.getClass());
    // System.out.println(t.getName());
}

From source file:cn.newtouch.model.TeacherTest.java

License:Open Source License

@Test
public void testUpdate1() {

    Session session = HibernateUtil.getSession();
    session.beginTransaction();//from   ww  w  . java2 s.c  o m
    Teacher t = (Teacher) session.get(Teacher.class, 1);

    session.getTransaction().commit();

    t.setName("zhanglaoshi");

    Session session2 = HibernateUtil.getSession();
    session2.beginTransaction();
    session2.update(t);

    session2.getTransaction().commit();
}

From source file:cn.newtouch.model.TeacherTest.java

License:Open Source License

@Test
public void testUpdate4() {

    Session session = HibernateUtil.getSession();
    session.beginTransaction();/*from  w ww .  j a v a  2s  .  c o m*/
    Teacher t = (Teacher) session.get(Teacher.class, 1);
    t.setName("zhangsan2");
    session.getTransaction().commit();
}

From source file:cn.newtouch.model.TeacherTest.java

License:Open Source License

@Test
public void testUpdate5() {

    Session session = HibernateUtil.getSession();
    session.beginTransaction();/*from ww w.  j  a  va  2s. c om*/
    Student s = (Student) session.get(Student.class, 1);
    s.setName("zhangsan5");
    session.getTransaction().commit();

    s.setName("z4");

    Session session2 = HibernateUtil.getSession();
    session2.beginTransaction();
    session2.update(s);
    session2.getTransaction().commit();
}