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: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#addUnRegisteredLog()}
 *//*from w  w  w  .j a v a2s . co  m*/
@Test
public void testAddUnRegisteredLog() {
    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(false);
    ses.save(rig);
    ses.getTransaction().commit();

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

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

    assertEquals(RigLog.OFFLINE, loaded.getOldState());
    assertEquals(RigLog.NOT_REGISTERED, loaded.getNewState());
    assertEquals("Un 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.RigTypeDaoTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.RigTypeDao#loadOrCreate(String)}
 *//*from  w ww.  j ava  2 s . c om*/
@Test
public void testLoadOrCreateNotExist() {
    RigType type = this.dao.loadOrCreate("testType");
    assertNotNull(type);
    assertEquals("testType", type.getName());
    assertFalse(type.isCodeAssignable());
    assertEquals(180, type.getLogoffGraceDuration());

    Session ses = DataAccessActivator.getNewSession();
    RigType same = (RigType) ses.load(RigType.class, type.getId());

    assertEquals(type.getName(), same.getName());
    assertEquals(type.getLogoffGraceDuration(), same.getLogoffGraceDuration());
    assertEquals(type.isCodeAssignable(), same.isCodeAssignable());

    this.dao.delete(type);
}

From source file:automatedbillingsoftware_DA.TaxDA.java

public void deleteTax(int id) {
    SessionFactory sessionFactory = HibernateUtils.getLocSessionFactory();
    Session session = sessionFactory.getCurrentSession();
    Transaction beginTransaction = session.beginTransaction();
    Tax tax = (Tax) session.load(Tax.class, id);
    tax.setStatus(0);/*from   w  ww . j a v a 2 s. co  m*/
    session.update(tax);
    beginTransaction.commit();
}

From source file:backend.api.FinanceManagement.java

public boolean removeUser(int idUser) // ok
{
    Session session = sf.openSession();
    try {//from w w  w.java2  s . c om
        Transaction tx = session.beginTransaction();
        Users user = (Users) session.load(Users.class, idUser);
        if (null != user) {
            session.delete(user);
            tx.commit();
            return true;
        } else {
            return false;
        }

    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        session.close();
    }

}

From source file:backend.api.FinanceManagement.java

public boolean removeGroup(int idGroup) // ok
{
    Session session = sf.openSession();
    try {/*w  w  w.j a  v a  2 s.c  om*/
        Transaction tx = session.beginTransaction();
        Groups group = (Groups) session.load(Groups.class, idGroup);
        if (null != group) {
            session.delete(group);
            tx.commit();
            return true;
        } else {
            return false;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        session.close();
    }
}

From source file:backend.api.StorageManagement.java

public boolean removeProduct(int idProduct) //ok
{
    Session session = SessionFactoryManager.INSTANCE.getSessionFactory().openSession();
    try {//w  w  w  .  java2s . c  om
        Transaction tx = session.beginTransaction();
        Products product = (Products) session.load(Products.class, idProduct);
        if (null != product) {
            session.delete(product);
            tx.commit();
            return true;
        } else {
            return false;
        }
    } finally {
        session.close();
    }
}

From source file:backend.api.StorageManagement.java

public boolean removeCategory(int idCategory) //ok
{
    Session session = SessionFactoryManager.INSTANCE.getSessionFactory().openSession();
    try {/*www  .j  a v a2s  .c  om*/
        Transaction tx = session.beginTransaction();
        Category category = (Category) session.load(Category.class, idCategory);
        if (null != category) {
            session.delete(category);
            tx.commit();
            return true;
        } else {
            return false;
        }
    } finally {
        session.close();
    }
}

From source file:backend.api.StorageManagement.java

public boolean removeManufacturer(int idManufacturer) //ok
{
    Session session = SessionFactoryManager.INSTANCE.getSessionFactory().openSession();
    try {//from   ww w .jav a 2  s .  c  o  m
        Transaction tx = session.beginTransaction();
        Manufacturers manu = (Manufacturers) session.load(Manufacturers.class, idManufacturer);
        if (null != manu) {
            session.delete(manu);
            tx.commit();
            return true;
        } else {
            return false;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

    finally {
        session.close();
    }
}

From source file:backend.api.StorageManagement.java

public boolean removeAttributes(int idAttributes) //ok
{
    Session session = SessionFactoryManager.INSTANCE.getSessionFactory().openSession();
    try {//from www  .j a  va2s.co m
        Transaction tx = session.beginTransaction();
        Category category = (Category) session.load(Category.class, idAttributes);
        if (null != category) {
            session.delete(category);
            tx.commit();
            return true;
        } else {
            return false;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

    finally {
        session.close();
    }
}

From source file:baking.dao.BaseDao.java

License:Open Source License

public Object load(Class clazz, Integer id) {
    try {/*from   w  w  w .  ja v a  2  s . c  o  m*/
        Session session = getSession();
        return session.load(clazz, id);
    } catch (RuntimeException re) {
        throw re;
    }
}