Example usage for org.hibernate Session load

List of usage examples for org.hibernate Session load

Introduction

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

Prototype

void load(Object object, Serializable id);

Source Link

Document

Read the persistent state associated with the given identifier into the given transient instance.

Usage

From source file:aseguradora.HospitalVentana.java

private void actualizarHospital() {
    int row = tablaHospital.getSelectedRow();
    try {//from  w w  w.j  av a2s.  co m

        Session session = sesion.openSession();
        Transaction tx = session.beginTransaction();
        Hospital hospital = new Hospital();
        Short cod_h = (row == -1) ? Short.parseShort(etCodHospital.getText().toString())
                : Short.parseShort(tablaHospital.getModel().getValueAt(row, 0).toString());
        hospital = (Hospital) session.load(Hospital.class, cod_h);
        hospital.setNH(etNomHospital.getText().toString());
        hospital.setNumC(Short.parseShort(etNumC.getText().toString()));
        session.update(hospital);
        tx.commit();
        session.close();
        cargarHospital();
    } catch (HibernateException e) {
        e.printStackTrace();
        if (e.getMessage().contains("" + CLAVE_PRIMARIA_DUPLICADA)) {
            JOptionPane.showMessageDialog(rootPane, "Existe un hospital con ese identificador (ID)");
        }
        if (e.getMessage().contains("" + VALOR_DEMASIADO_LARGO)) {
            JOptionPane.showMessageDialog(rootPane, "Has introducido un valor demasiado largo");
        }
        ;
        if (e.getMessage().contains("" + ERROR_EDITAR_CLAVE_AJENA)) {
            JOptionPane.showMessageDialog(rootPane, "No puedes editar el id de este hospital");
        }
        ;
    }
}

From source file:aseguradora.HospitalVentana.java

private void eliminarHospital() {
    int row = tablaHospital.getSelectedRow();
    try {/*from   www  .  j a v a2  s.c o  m*/

        Session session = sesion.openSession();
        Transaction tx = session.beginTransaction();
        Hospital hospital = new Hospital();
        Short cod_h = (row == -1) ? Short.parseShort(etCodHospital.getText().toString())
                : Short.parseShort(tablaHospital.getModel().getValueAt(row, 0).toString());
        hospital = (Hospital) session.load(Hospital.class, cod_h);
        session.delete(hospital);
        tx.commit();
        session.close();
        cargarHospital();
    } catch (HibernateException e) {
        e.printStackTrace();
        if (e.getMessage().contains("" + ERROR_EDITAR_CLAVE_AJENA)) {
            JOptionPane.showMessageDialog(rootPane,
                    "No puedes borrar este hospital, restriccin clave primeria/ajena");
        }
    }
}

From source file:aseguradora.MedicoVentana.java

private void actualizarMedico() {
    int row = tablaMedico.getSelectedRow();
    try {/*from ww w.  ja  v a  2  s  .  c  o m*/

        Session session = sesion.openSession();
        Transaction tx = session.beginTransaction();
        Medico medico = new Medico();
        Short cod_m = (row == -1) ? Short.parseShort(etCodMedico.getText().toString())
                : Short.parseShort(tablaMedico.getModel().getValueAt(row, 0).toString());
        Hospital hospital = new Hospital(Short.parseShort(etCodHospital.getText().toString()));
        medico = (Medico) session.load(Medico.class, cod_m);
        medico.setNM(etNomMedico.getText().toString());
        medico.setHospital(hospital);
        session.update(medico);
        tx.commit();
        session.close();
        cargarMedico();
    } catch (JDBCException e) {
        e.printStackTrace();
        if (e.getMessage().contains("" + CLAVE_PRIMARIA_DUPLICADA)) {
            JOptionPane.showMessageDialog(rootPane, "Existe un mdico con ese identificador (ID)");
        }
        if (e.getMessage().contains("" + CLAVE_AJENA_NO_ENCONTRADA)) {
            JOptionPane.showMessageDialog(rootPane, "No existe un hospital con ese identificador (ID)");
        }
        if (e.getMessage().contains("" + VALOR_DEMASIADO_LARGO)) {
            JOptionPane.showMessageDialog(rootPane, "Has introducido un valor demasiado largo");
        }
        ;
        if (e.getMessage().contains("" + ERROR_EDITAR_CLAVE_AJENA)) {
            JOptionPane.showMessageDialog(rootPane, "No puedes editar el id de este mdico");
        }
        ;
    }
}

From source file:aseguradora.MedicoVentana.java

private void eliminarMedico() {
    int row = tablaMedico.getSelectedRow();
    try {/* w  w w  . ja v a  2  s  . c o m*/

        Session session = sesion.openSession();
        Transaction tx = session.beginTransaction();
        Medico medico = new Medico();
        Short cod_m = (row == -1) ? Short.parseShort(etCodMedico.getText().toString())
                : Short.parseShort(tablaMedico.getModel().getValueAt(row, 0).toString());
        medico = (Medico) session.load(Medico.class, cod_m);
        session.delete(medico);
        tx.commit();
        session.close();
        cargarMedico();
    } catch (JDBCException e) {
        System.out.println("aqui");
        e.printStackTrace();
        if (e.getMessage().contains("" + ERROR_EDITAR_CLAVE_AJENA)) {
            JOptionPane.showMessageDialog(rootPane,
                    "No puedes borrar este medico, restriccin clave primeria/ajena");
        }
    }
}

From source file:at.molindo.esi4j.module.hibernate.HibernateEntityResolver.java

License:Apache License

@Override
public Object resolveEntity(Object replacedEntity) {
    if (replacedEntity instanceof ObjectKey) {
        ObjectKey key = (ObjectKey) replacedEntity;

        Session session = _localSession.get();
        if (session == null) {
            throw new IllegalStateException("no session available");
        }//from  www  . j  av  a2 s  . c  om

        // ignore version, use latest, use load for batch fetching
        Object resolvedEntity = session.load(key.getType(), key.getId());

        if (resolvedEntity == null) {
            log.error("can't resolve object " + key);
            return null;
        }

        return resolvedEntity;
    } else {
        // not replaced
        return replacedEntity;
    }

}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.GenericDaoTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.GenericDao#flush()}.
 *///www  .j  a  va 2  s .  c  o m
public void testFlush() {
    Config conf = this.dao.persist(new Config("flush_conf_key", "val"));
    assertNotNull(conf);

    /* Make dirty. */
    conf.setValue("new_val");

    Session ses = this.dao.getSession();
    assertNotNull(ses);
    assertTrue(ses.isDirty());

    /* Close which should flush. */
    this.dao.flush();
    assertFalse(ses.isDirty());

    ses = DataAccessActivator.getNewSession();
    Config c = (Config) ses.load(Config.class, conf.getId());
    assertEquals("new_val", c.getValue());

    ses.getTransaction().begin();
    ses.delete(c);
    ses.getTransaction().commit();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.GenericDaoTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.GenericDao#closeSession()}.
 *///from   www.  j a  v a2  s  . c om
public void testCloseSession() {
    Config conf = this.dao.persist(new Config("close_conf_key", "val"));
    assertNotNull(conf);

    /* Make dirty. */
    conf.setValue("new_val");

    Session ses = this.dao.getSession();
    assertNotNull(ses);
    assertTrue(ses.isOpen());
    assertTrue(ses.isDirty());

    /* Close which should flush. */
    this.dao.closeSession();
    assertFalse(ses.isOpen());

    ses = DataAccessActivator.getNewSession();
    Config c = (Config) ses.load(Config.class, conf.getId());
    assertEquals("new_val", c.getValue());

    ses.beginTransaction();
    ses.delete(c);
    ses.getTransaction().commit();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.RigLogDaoTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.RigLogDao#addRegisteredLog()}
 *//*  ww w .j a v a 2s  . c o  m*/
@Test
public void testAddRegisteredLog() {
    Date now = new Date();

    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();
    RigType type = new RigType();
    type.setName("rig_test");
    type.setLogoffGraceDuration(600);
    ses.save(type);

    RigCapabilities caps = new RigCapabilities("a,b,c,d,e,f");
    ses.save(caps);

    Rig rig = new Rig();
    rig.setName("rig_name_test");
    rig.setRigType(type);
    rig.setContactUrl("http://url");
    rig.setRigCapabilities(caps);
    rig.setLastUpdateTimestamp(now);
    rig.setManaged(false);
    rig.setMeta("iLabs");
    rig.setOnline(true);
    ses.save(rig);
    ses.getTransaction().commit();

    RigLogDao dao = new RigLogDao(ses);
    RigLog log = dao.addRegisteredLog(rig, "Newly registered");
    assertNotNull(log);

    Session ses2 = DataAccessActivator.getNewSession();
    RigLog loaded = (RigLog) ses2.load(RigLog.class, log.getId());
    assertNotNull(loaded);

    assertEquals(RigLog.NOT_REGISTERED, loaded.getOldState());
    assertEquals(RigLog.ONLINE, loaded.getNewState());
    assertEquals("Newly registered", loaded.getReason());
    assertEquals(Math.floor(now.getTime() / 1000), Math.floor(loaded.getTimeStamp().getTime() / 1000));
    assertEquals(rig.getId().longValue(), loaded.getRig().getId().longValue());

    ses.beginTransaction();
    ses.delete(log);
    ses.delete(rig);
    ses.delete(type);
    ses.delete(caps);
    ses.getTransaction().commit();
    ses.close();
    ses2.close();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.RigLogDaoTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.RigLogDao#addOnlineLog()}
 *//*w  ww . j  a v  a 2s  .c  om*/
@Test
public void testAddOnlineLog() {
    Date now = new Date();

    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();
    RigType type = new RigType();
    type.setName("rig_test");
    type.setLogoffGraceDuration(600);
    ses.save(type);

    RigCapabilities caps = new RigCapabilities("a,b,c,d,e,f");
    ses.save(caps);

    Rig rig = new Rig();
    rig.setName("rig_name_test");
    rig.setRigType(type);
    rig.setContactUrl("http://url");
    rig.setRigCapabilities(caps);
    rig.setLastUpdateTimestamp(now);
    rig.setManaged(false);
    rig.setMeta("iLabs");
    rig.setOnline(true);
    ses.save(rig);
    ses.getTransaction().commit();

    RigLogDao dao = new RigLogDao(ses);
    RigLog log = dao.addOnlineLog(rig, "Fixed");
    assertNotNull(log);

    Session ses2 = DataAccessActivator.getNewSession();
    RigLog loaded = (RigLog) ses2.load(RigLog.class, log.getId());
    assertNotNull(loaded);

    assertEquals(RigLog.OFFLINE, loaded.getOldState());
    assertEquals(RigLog.ONLINE, loaded.getNewState());
    assertEquals("Fixed", loaded.getReason());
    assertEquals(Math.floor(now.getTime() / 1000), Math.floor(loaded.getTimeStamp().getTime() / 1000));
    assertEquals(rig.getId().longValue(), loaded.getRig().getId().longValue());

    ses.beginTransaction();
    ses.delete(log);
    ses.delete(rig);
    ses.delete(type);
    ses.delete(caps);
    ses.getTransaction().commit();
    ses.close();
    ses2.close();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.RigLogDaoTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.RigLogDao#addOfflineLog()}
 *//*from w  w  w. j a va2s  . c  o m*/
@Test
public void testAddOfflineLog() {
    Date now = new Date();

    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();
    RigType type = new RigType();
    type.setName("rig_test");
    type.setLogoffGraceDuration(600);
    ses.save(type);

    RigCapabilities caps = new RigCapabilities("a,b,c,d,e,f");
    ses.save(caps);

    Rig rig = new Rig();
    rig.setName("rig_name_test");
    rig.setRigType(type);
    rig.setContactUrl("http://url");
    rig.setRigCapabilities(caps);
    rig.setLastUpdateTimestamp(now);
    rig.setManaged(false);
    rig.setMeta("iLabs");
    rig.setOnline(true);
    ses.save(rig);
    ses.getTransaction().commit();

    RigLogDao dao = new RigLogDao(ses);
    RigLog log = dao.addOfflineLog(rig, "Broken");
    assertNotNull(log);

    Session ses2 = DataAccessActivator.getNewSession();
    RigLog loaded = (RigLog) ses2.load(RigLog.class, log.getId());
    assertNotNull(loaded);

    assertEquals(RigLog.ONLINE, loaded.getOldState());
    assertEquals(RigLog.OFFLINE, loaded.getNewState());
    assertEquals("Broken", loaded.getReason());
    assertEquals(Math.floor(now.getTime() / 1000), Math.floor(loaded.getTimeStamp().getTime() / 1000));
    assertEquals(rig.getId().longValue(), loaded.getRig().getId().longValue());

    ses.beginTransaction();
    ses.delete(log);
    ses.delete(rig);
    ses.delete(type);
    ses.delete(caps);
    ses.getTransaction().commit();
    ses.close();
    ses2.close();
}