Example usage for org.hibernate Session delete

List of usage examples for org.hibernate Session delete

Introduction

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

Prototype

void delete(Object object);

Source Link

Document

Remove a persistent instance from the datastore.

Usage

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()}.
 *///from  w  ww . j av  a  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  w  w w.j av a  2  s. c o m*/
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.RequestCapabilitiesDaoTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.RigCapabilitiesDao#addCapabilities(java.lang.String)}.
 *//*from   w  w  w. jav  a2  s .c om*/
@Test
public void testAddCapabilities() {
    /* Add the request capabilities. */
    RigCapabilities req[] = new RigCapabilities[10];
    RigCapabilitiesDao rigDao = new RigCapabilitiesDao();
    req[0] = new RigCapabilities("a,b,c,d");
    req[1] = new RigCapabilities("a,b,c,d,e");
    req[2] = new RigCapabilities("a,d,e");
    req[3] = new RigCapabilities("a,b,d,f");
    req[4] = new RigCapabilities("b,d,f");
    req[5] = new RigCapabilities("f");
    req[6] = new RigCapabilities("a,c,f");
    req[7] = new RigCapabilities("x,y");
    req[8] = new RigCapabilities("xy,x");
    req[9] = new RigCapabilities("x,y,z");
    for (int i = 0; i < req.length; i++) {
        req[i] = rigDao.persist(req[i]);
    }

    String capsStr = "d, a,b";
    RequestCapabilities caps = this.dao.addCapabilities(capsStr);
    assertNotNull(caps);
    assertEquals("a,b,d", caps.getCapabilities());

    Set<MatchingCapabilities> matches = caps.getMatchingCapabilitieses();
    List<String> matchingReq = new ArrayList<String>();
    for (MatchingCapabilities m : matches) {
        matchingReq.add(m.getRigCapabilities().getCapabilities());
    }

    assertEquals(3, matchingReq.size());
    assertTrue(matchingReq.contains("a,b,c,d"));
    assertTrue(matchingReq.contains("a,b,c,d,e"));
    assertTrue(matchingReq.contains("a,b,d,f"));

    Session ses = this.dao.getSession();
    ses.beginTransaction();
    for (MatchingCapabilities m : matches) {
        ses.delete(m);
    }
    ses.getTransaction().commit();
    for (RigCapabilities r : req) {
        rigDao.delete(r);
    }
    this.dao.delete(caps);
}

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

License:Open Source License

@Test
public void testDelete() {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();/*from   w  w  w. j av a 2 s .  co m*/
    RequestCapabilities req = new RequestCapabilities("a,b");
    ses.persist(req);
    RigCapabilities rig1 = new RigCapabilities("a,b,c,d");
    ses.persist(rig1);
    RigCapabilities rig2 = new RigCapabilities("a,b,c,d,e");
    ses.persist(rig2);
    MatchingCapabilities match1 = new MatchingCapabilities(
            new MatchingCapabilitiesId(rig1.getId(), req.getId()), req, rig1);
    ses.persist(match1);
    MatchingCapabilities match2 = new MatchingCapabilities(
            new MatchingCapabilitiesId(rig2.getId(), req.getId()), req, rig2);
    ses.persist(match2);
    ses.getTransaction().commit();

    this.dao.delete(req.getId());
    assertNull(this.dao.get(req.getId()));

    ses.refresh(rig1);
    assertEquals(0, rig1.getMatchingCapabilitieses().size());
    ses.refresh(rig2);
    assertEquals(0, rig2.getMatchingCapabilitieses().size());

    ses.beginTransaction();
    ses.delete(rig1);
    ses.delete(rig2);
    ses.getTransaction().commit();
}

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

License:Open Source License

public void testDeleteResourcePermission() {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();// w w w.  j  a v a 2  s. co m
    RequestCapabilities req = new RequestCapabilities("a,b");
    ses.persist(req);
    UserClass uc = new UserClass();
    uc.setName("user_class");
    ses.persist(uc);
    RigCapabilities rig1 = new RigCapabilities("a,b,c,d");
    ses.persist(rig1);
    RigCapabilities rig2 = new RigCapabilities("a,b,c,d,e");
    ses.persist(rig2);
    ResourcePermission rc = new ResourcePermission();
    rc.setType("CAPABILITY");
    rc.setRequestCapabilities(req);
    rc.setExpiryTime(new Date());
    rc.setStartTime(new Date());
    rc.setUserClass(uc);
    ses.persist(rc);
    MatchingCapabilities match1 = new MatchingCapabilities(
            new MatchingCapabilitiesId(rig1.getId(), req.getId()), req, rig1);
    ses.persist(match1);
    MatchingCapabilities match2 = new MatchingCapabilities(
            new MatchingCapabilitiesId(rig2.getId(), req.getId()), req, rig2);
    ses.persist(match2);
    ses.getTransaction().commit();

    this.dao.delete(req.getId());
    assertNull(this.dao.get(req.getId()));

    ses.refresh(rig1);
    assertEquals(0, rig1.getMatchingCapabilitieses().size());
    ses.refresh(rig2);
    assertEquals(0, rig2.getMatchingCapabilitieses().size());

    ses.beginTransaction();
    ses.delete(rig1);
    ses.delete(rig2);
    ses.delete(rc);
    ses.delete(uc);
    ses.getTransaction().commit();
}

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

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.RigCapabilitiesDao#addCapabilities(java.lang.String)}.
 *///w w w  .j av  a 2  s. co  m
@Test
public void testAddCapabilities() {
    /* Add the request capabilities. */
    RequestCapabilities req[] = new RequestCapabilities[10];
    GenericDao<RequestCapabilities> reqDao = new GenericDao<RequestCapabilities>(this.dao.getSession(),
            RequestCapabilities.class);
    req[0] = new RequestCapabilities("a,b");
    req[1] = new RequestCapabilities("b,c");
    req[2] = new RequestCapabilities("a,c");
    req[3] = new RequestCapabilities("c,f");
    req[4] = new RequestCapabilities("d,f");
    req[5] = new RequestCapabilities("f");
    req[6] = new RequestCapabilities("a,c,f");
    req[7] = new RequestCapabilities("x,y");
    req[8] = new RequestCapabilities("xy,x");
    req[9] = new RequestCapabilities("x,y,z");
    for (int i = 0; i < req.length; i++) {
        req[i] = reqDao.persist(req[i]);
    }

    String capsStr = "f, d, a, b ";
    RigCapabilities caps = this.dao.addCapabilities(capsStr);
    assertNotNull(caps);
    assertEquals("a,b,d,f", caps.getCapabilities());

    Set<MatchingCapabilities> matches = caps.getMatchingCapabilitieses();
    List<String> matchingReq = new ArrayList<String>();
    for (MatchingCapabilities m : matches) {
        matchingReq.add(m.getRequestCapabilities().getCapabilities());
    }

    assertEquals(3, matchingReq.size());
    assertTrue(matchingReq.contains("a,b"));
    assertTrue(matchingReq.contains("d,f"));
    assertTrue(matchingReq.contains("f"));

    Session ses = this.dao.getSession();
    ses.beginTransaction();
    for (MatchingCapabilities m : matches) {
        ses.delete(m);
    }
    ses.getTransaction().commit();
    for (RequestCapabilities r : req) {
        reqDao.delete(r);
    }
    this.dao.delete(caps);
}

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

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.RigCapabilitiesDao#addCapabilities(java.lang.String)}.
 *///  w  ww  .ja v  a 2 s.  com
@Test
public void testAddOneCapabilities() {
    /* Add the request capabilities. */
    RequestCapabilities req = new RequestCapabilities("b");
    GenericDao<RequestCapabilities> reqDao = new GenericDao<RequestCapabilities>(this.dao.getSession(),
            RequestCapabilities.class);
    reqDao.persist(req);

    String capsStr = "a,b";
    RigCapabilities caps = this.dao.addCapabilities(capsStr);
    assertNotNull(caps);
    assertEquals("a,b", caps.getCapabilities());

    Set<MatchingCapabilities> matches = caps.getMatchingCapabilitieses();
    assertEquals(1, matches.size());
    MatchingCapabilities mc = matches.iterator().next();
    assertNotNull(mc);
    assertEquals(mc.getId().getRequestCapabilities(), req.getId().longValue());
    assertEquals(mc.getId().getRigCapabilities(), caps.getId().longValue());

    Session ses = this.dao.getSession();
    ses.beginTransaction();
    for (MatchingCapabilities m : matches) {
        ses.delete(m);
    }
    ses.getTransaction().commit();
    ses.delete(req);
    this.dao.delete(caps);
}

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

License:Open Source License

@Test
public void testDelete() {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();/* w w  w . jav a  2s . com*/
    RequestCapabilities req1 = new RequestCapabilities("a,b");
    ses.persist(req1);
    RequestCapabilities req2 = new RequestCapabilities("c,d");
    ses.persist(req2);
    RigCapabilities rig = new RigCapabilities("a,b,c,d");
    ses.persist(rig);
    MatchingCapabilities match1 = new MatchingCapabilities(
            new MatchingCapabilitiesId(rig.getId(), req1.getId()), req1, rig);
    ses.persist(match1);
    MatchingCapabilities match2 = new MatchingCapabilities(
            new MatchingCapabilitiesId(rig.getId(), req2.getId()), req2, rig);
    ses.persist(match2);
    ses.getTransaction().commit();

    this.dao.delete(rig.getId());
    assertNull(this.dao.get(rig.getId()));

    ses.refresh(req1);
    assertEquals(0, req1.getMatchingCapabilitieses().size());
    ses.refresh(req2);
    assertEquals(0, req2.getMatchingCapabilitieses().size());

    ses.beginTransaction();
    ses.delete(req1);
    ses.delete(req2);
    ses.getTransaction().commit();
}

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

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.RigDao#findByName(java.lang.String)}.
 *//*from www . java 2s  . c o  m*/
@Test
public void testFindByName() {
    /* Add a rig. */
    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);
    Timestamp ts = new Timestamp(System.currentTimeMillis());
    ts.setNanos(0); // Need to trunc time as nanoseconds aren't stored in the DB.
    rig.setLastUpdateTimestamp(ts);
    rig.setManaged(false);
    rig.setMeta("iLabs");
    rig.setOfflineReason("Tomorrows problem");
    ses.save(rig);
    ses.getTransaction().commit();
    ses.close();

    /* Find the rig. */
    Rig fRig = this.dao.findByName("rig_name_test");
    assertNotNull(fRig);
    assertEquals(rig.getId(), fRig.getId());
    assertEquals(rig.getContactUrl(), fRig.getContactUrl());
    assertEquals(fRig.getLastUpdateTimestamp(), rig.getLastUpdateTimestamp());

    assertEquals(rig.getRigType().getName(), fRig.getRigType().getName());
    assertEquals(rig.getRigCapabilities().getCapabilities(), fRig.getRigCapabilities().getCapabilities());
    assertEquals(rig.isManaged(), fRig.isManaged());
    assertEquals(rig.getMeta(), fRig.getMeta());
    assertEquals(rig.getOfflineReason(), fRig.getOfflineReason());

    /* Delete all. */
    this.dao.delete(fRig);
    ses = this.dao.getSession();
    ses.beginTransaction();
    ses.delete(fRig.getRigCapabilities());
    ses.delete(fRig.getRigType());
    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()}
 *///from  ww w  .ja v a  2 s .  com
@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();
}