Example usage for org.hibernate Session createQuery

List of usage examples for org.hibernate Session createQuery

Introduction

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

Prototype

@Override
    org.hibernate.query.Query createQuery(CriteriaDelete deleteQuery);

Source Link

Usage

From source file:au.edu.anu.metadatastores.store.search.DBSearch.java

License:Open Source License

/**
 * Execute the query with the given parameters
 * /* w  w  w  . j  av  a 2  s  .  c  o m*/
 * @param session The hibernate session
 * @param queryString The query string
 * @param parameters The parameters
 * @return The list of items resulting from the query
 */
private List<ItemDTO> executeQueryWithParameters(Session session, String queryString, List<String> parameters) {
    Query query = session.createQuery(queryString);
    for (int i = 0; i < parameters.size(); i++) {
        query.setParameter(i, parameters.get(i));
    }
    @SuppressWarnings("unchecked")
    List<ItemDTO> items = query.list();
    return items;
}

From source file:au.edu.anu.metadatastores.store.search.SearchService.java

License:Open Source License

/**
 * Get the system types (i.e. the types for Publications, Grants, People, etc.)
 * /*from w  ww  . j a  v  a2s .  c om*/
 * @return The system types
 */
public List<SystemType> getSystemTypes() {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        Query query = session.createQuery("FROM SystemType");
        @SuppressWarnings("unchecked")
        List<SystemType> systemTypes = query.list();
        return systemTypes;
    } finally {
        session.close();
    }
}

From source file:au.edu.anu.metadatastores.store.search.SearchService.java

License:Open Source License

/**
 * Get the attribute types (i.e. the types for Title, Given Name, Year, etc.)
 * //from  ww  w.j a v  a  2  s  .co  m
 * @return The attribute types
 */
public List<AttributeType> getAttributeTypes() {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();

    try {
        Query query = session.createQuery("FROM AttributeType");
        @SuppressWarnings("unchecked")
        List<AttributeType> attributeTypes = query.list();
        return attributeTypes;
    } finally {
        session.close();
    }
}

From source file:au.edu.anu.metadatastores.store.search.SearchService.java

License:Open Source License

/**
 * Execute the relationship query//  w  ww  . ja  va  2s.  c o m
 * 
 * @param session The hibernate session
 * @param queryString The query string
 * @param id The id of the object
 * @param system The system to filter the relation on if it exists
 * @return The related items
 */
private List<ItemDTO> executeRelationQuery(Session session, String queryString, Long id, String system) {
    Query query = session.createQuery(queryString.toString());
    query.setParameter("id", id);
    if (system != null && system.length() > 0) {
        query.setParameter("system", system);
    }

    @SuppressWarnings("unchecked")
    List<ItemDTO> items = query.list();
    return items;
}

From source file:au.edu.uts.eng.remotelabs.schedserver.rigprovider.impl.tests.RegisterLocalRigTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.rigprovider.impl.RegisterLocalRig#registerRig(java.lang.String, java.lang.String, java.lang.String, java.lang.String)}.
 *///w ww.j a v  a  2 s . c  om
@Test
public void testAddRigToSchedServerAllNew() {
    Session ses = DataAccessActivator.getNewSession();

    String name = "register1";
    String type = "registertest";
    String caps = "a,b,c,d,e,f";
    String contactUrl = "http://lbremote1.eng.uts.edu.au:7070/services/RigClientService";

    assertTrue(this.register.registerRig(name, type, caps, contactUrl));
    assertNull(this.register.getFailedReason());

    Rig rig = this.register.getRegisteredRig();
    assertNotNull(rig);
    this.register.getSession().close();

    assertEquals(name, rig.getName());
    assertEquals(type, rig.getRigType().getName());
    assertEquals(contactUrl, rig.getContactUrl());
    assertNotNull(rig.getRigCapabilities());

    /* Default values. */
    assertTrue(rig.isActive());
    assertFalse(rig.isInSession());
    assertFalse(rig.isOnline());
    assertTrue(rig.isManaged());
    assertNotNull(rig.getOfflineReason());
    assertNull(rig.getMeta());
    assertNull(rig.getSession());

    assertTrue(new Date(System.currentTimeMillis()).after(rig.getLastUpdateTimestamp()));
    assertTrue(new Date(System.currentTimeMillis() - 10000).before(rig.getLastUpdateTimestamp()));

    /* Test caps record was created and added. */
    RigCapabilities capsRec = new RigCapabilitiesDao(ses).findCapabilites(caps);
    assertNotNull(capsRec);
    assertEquals(caps, capsRec.getCapabilities());

    /* Test type record was created and added. */
    RigType typeRec = new RigTypeDao(ses).findByName(type);
    assertNotNull(typeRec);
    assertEquals(type, typeRec.getName());
    assertEquals(180, typeRec.getLogoffGraceDuration());
    assertFalse(typeRec.isCodeAssignable());

    /* Test rig record was created and added. */
    Rig rigRec = new RigDao(ses).findByName(name);
    assertNotNull(rigRec);
    assertNotNull(rigRec.getId());
    assertEquals(name, rigRec.getName());
    assertEquals(type, rigRec.getRigType().getName());
    assertTrue(rigRec.isActive());
    assertEquals(contactUrl, rigRec.getContactUrl());
    assertFalse(rigRec.isInSession());
    assertNotNull(rigRec.getLastUpdateTimestamp());
    assertTrue(rigRec.isManaged());
    assertNull(rigRec.getMeta());
    assertNotNull(rigRec.getOfflineReason());
    assertFalse(rigRec.isOnline());
    assertEquals(caps, rigRec.getRigCapabilities().getCapabilities());

    assertTrue(new Date(System.currentTimeMillis()).after(rig.getLastUpdateTimestamp()));
    assertTrue(new Date(System.currentTimeMillis() - 10000).before(rig.getLastUpdateTimestamp()));

    ses.beginTransaction();
    ses.createQuery("DELETE FROM RigLog WHERE rig='" + rig.getId() + "'").executeUpdate();
    ses.getTransaction().commit();

    ses.beginTransaction();
    ses.delete(rigRec);
    ses.delete(capsRec);
    ses.delete(typeRec);
    ses.getTransaction().commit();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.rigprovider.impl.tests.RegisterLocalRigTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.rigprovider.impl.RegisterLocalRig#registerRig(java.lang.String, java.lang.String, java.lang.String, java.lang.String)}.
 *///from   ww  w  . jav  a 2s.c o m
@Test
public void testAddRigToSchedServerNewRigExistingTypeNewCaps() {
    Session ses = DataAccessActivator.getNewSession();

    String name = "register1";
    String type = "registertest";
    String caps = "a,b,c,d,e,f";
    String contactUrl = "http://lbremote1.eng.uts.edu.au:7070/services/RigClientService";

    RigType existType = new RigType(type, 180, false);
    ses.beginTransaction();
    ses.save(existType);
    ses.getTransaction().commit();
    ses.flush();
    ses.evict(existType);

    assertTrue(this.register.registerRig(name, type, caps, contactUrl));
    assertNull(this.register.getFailedReason());

    Rig rig = this.register.getRegisteredRig();
    assertNotNull(rig);

    assertEquals(name, rig.getName());
    assertEquals(type, rig.getRigType().getName());
    assertEquals(contactUrl, rig.getContactUrl());
    assertNotNull(rig.getRigCapabilities());

    /* Default values. */
    assertTrue(rig.isActive());
    assertFalse(rig.isInSession());
    assertFalse(rig.isOnline());
    assertTrue(rig.isManaged());
    assertNotNull(rig.getOfflineReason());
    assertNull(rig.getMeta());
    assertNull(rig.getSession());

    assertTrue(new Date(System.currentTimeMillis()).after(rig.getLastUpdateTimestamp()));
    assertTrue(new Date(System.currentTimeMillis() - 10000).before(rig.getLastUpdateTimestamp()));

    /* Test caps record was created and added. */
    RigCapabilities capsRec = new RigCapabilitiesDao(ses).findCapabilites(caps);
    assertNotNull(capsRec);
    assertEquals(caps, capsRec.getCapabilities());

    /* Test type record was created and added. */
    RigType typeRec = new RigTypeDao(ses).findByName(type);
    assertNotNull(typeRec);
    assertEquals(type, typeRec.getName());
    assertEquals(180, typeRec.getLogoffGraceDuration());
    assertFalse(typeRec.isCodeAssignable());

    /* Test rig record was created and added. */
    Rig rigRec = new RigDao(ses).findByName(name);
    assertNotNull(rigRec);
    assertNotNull(rigRec.getId());
    assertEquals(name, rigRec.getName());
    assertEquals(type, rigRec.getRigType().getName());
    assertTrue(rigRec.isActive());
    assertEquals(contactUrl, rigRec.getContactUrl());
    assertFalse(rigRec.isInSession());
    assertNotNull(rigRec.getLastUpdateTimestamp());
    assertTrue(rigRec.isManaged());
    assertNull(rigRec.getMeta());
    assertNotNull(rigRec.getOfflineReason());
    assertFalse(rigRec.isOnline());
    assertEquals(caps, rigRec.getRigCapabilities().getCapabilities());

    assertTrue(new Date(System.currentTimeMillis()).after(rigRec.getLastUpdateTimestamp()));
    assertTrue(new Date(System.currentTimeMillis() - 10000).before(rigRec.getLastUpdateTimestamp()));

    /* Cleanup. */
    ses.beginTransaction();
    ses.createQuery("DELETE FROM RigLog WHERE rig='" + rig.getId() + "'").executeUpdate();
    ses.getTransaction().commit();

    ses.beginTransaction();
    ses.delete(rigRec);
    ses.delete(capsRec);
    ses.delete(typeRec);
    ses.getTransaction().commit();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.rigprovider.impl.tests.RegisterLocalRigTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.rigprovider.impl.RegisterLocalRig#registerRig(java.lang.String, java.lang.String, java.lang.String, java.lang.String)}.
 *//*  w  w w .j  av  a  2 s .c  o  m*/
@Test
public void testAddRigToSchedServerNewRigExistingTypeExistingCaps() {
    Session ses = DataAccessActivator.getNewSession();

    String name = "register1";
    String type = "registertest";
    String caps = "a,b,c,d,e,f";
    String contactUrl = "http://lbremote1.eng.uts.edu.au:7070/services/RigClientService";

    RigType existType = new RigType(type, 180, false);
    RigCapabilities existCaps = new RigCapabilities(caps);
    ses.beginTransaction();
    ses.save(existType);
    ses.save(existCaps);
    ses.getTransaction().commit();
    ses.flush();
    ses.evict(existType);

    assertTrue(this.register.registerRig(name, type, caps, contactUrl));
    assertNull(this.register.getFailedReason());

    Rig rig = this.register.getRegisteredRig();
    assertNotNull(rig);

    assertEquals(name, rig.getName());
    assertEquals(type, rig.getRigType().getName());
    assertEquals(contactUrl, rig.getContactUrl());
    assertNotNull(rig.getRigCapabilities());

    /* Default values. */
    assertTrue(rig.isActive());
    assertFalse(rig.isInSession());
    assertFalse(rig.isOnline());
    assertTrue(rig.isManaged());
    assertNotNull(rig.getOfflineReason());
    assertNull(rig.getMeta());
    assertNull(rig.getSession());

    assertTrue(new Date(System.currentTimeMillis()).after(rig.getLastUpdateTimestamp()));
    assertTrue(new Date(System.currentTimeMillis() - 10000).before(rig.getLastUpdateTimestamp()));

    /* Test caps record was created and added. */
    RigCapabilities capsRec = new RigCapabilitiesDao(ses).findCapabilites(caps);
    assertNotNull(capsRec);
    assertEquals(caps, capsRec.getCapabilities());

    /* Test type record was created and added. */
    RigType typeRec = new RigTypeDao(ses).findByName(type);
    assertNotNull(typeRec);
    assertEquals(type, typeRec.getName());
    assertEquals(180, typeRec.getLogoffGraceDuration());
    assertFalse(typeRec.isCodeAssignable());

    /* Test rig record was created and added. */
    Rig rigRec = new RigDao(ses).findByName(name);
    assertNotNull(rigRec);
    assertNotNull(rigRec.getId());
    assertEquals(name, rigRec.getName());
    assertEquals(type, rigRec.getRigType().getName());
    assertTrue(rigRec.isActive());
    assertEquals(contactUrl, rigRec.getContactUrl());
    assertFalse(rigRec.isInSession());
    assertNotNull(rigRec.getLastUpdateTimestamp());
    assertTrue(rigRec.isManaged());
    assertNull(rigRec.getMeta());
    assertNotNull(rigRec.getOfflineReason());
    assertFalse(rigRec.isOnline());
    assertEquals(caps, rigRec.getRigCapabilities().getCapabilities());

    assertTrue(new Date(System.currentTimeMillis()).after(rigRec.getLastUpdateTimestamp()));
    assertTrue(new Date(System.currentTimeMillis() - 10000).before(rigRec.getLastUpdateTimestamp()));

    /* Cleanup. */
    ses.beginTransaction();
    ses.createQuery("DELETE FROM RigLog WHERE rig='" + rig.getId() + "'").executeUpdate();
    ses.getTransaction().commit();

    ses.beginTransaction();
    ses.delete(rigRec);
    ses.delete(capsRec);
    ses.delete(typeRec);
    ses.getTransaction().commit();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.rigprovider.impl.tests.RegisterLocalRigTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.rigprovider.impl.RegisterLocalRig#registerRig(java.lang.String, java.lang.String, java.lang.String, java.lang.String)}.
 *///from   w  w w  .j a  va 2s . c  om
@Test
public void testAddRigToSchedServerInactiveRig() {
    Session ses = DataAccessActivator.getNewSession();

    String name = "register1";
    String type = "registertest";
    String caps = "a,b,c,d,e,f";
    String contactUrl = "http://lbremote1.eng.uts.edu.au:7070/services/RigClientService";

    RigType existType = new RigType(type, 180, false);
    RigCapabilities existCaps = new RigCapabilities(caps);
    ses.beginTransaction();
    ses.save(existType);
    ses.save(existCaps);
    ses.getTransaction().commit();
    ses.flush();
    ses.evict(existType);
    ses.evict(existCaps);

    Rig existRig = new Rig(existType, existCaps, name, contactUrl, new Date(), false, "Broken.", false, true,
            false);
    ses.beginTransaction();
    ses.save(existRig);
    ses.getTransaction().commit();
    ses.flush();
    ses.evict(existRig);

    assertTrue(this.register.registerRig(name, type, caps, contactUrl));
    assertNull(this.register.getFailedReason());

    Rig rig = this.register.getRegisteredRig();
    assertNotNull(rig);

    assertEquals(name, rig.getName());
    assertEquals(type, rig.getRigType().getName());
    assertEquals(contactUrl, rig.getContactUrl());
    assertNotNull(rig.getRigCapabilities());

    /* Default values. */
    assertTrue(rig.isActive());
    assertFalse(rig.isInSession());
    assertFalse(rig.isOnline());
    assertTrue(rig.isManaged());
    assertNotNull(rig.getOfflineReason());
    assertNull(rig.getMeta());
    assertNull(rig.getSession());

    assertTrue(new Date(System.currentTimeMillis()).after(rig.getLastUpdateTimestamp()));
    assertTrue(new Date(System.currentTimeMillis() - 10000).before(rig.getLastUpdateTimestamp()));

    /* Test caps record was created and added. */
    RigCapabilities capsRec = new RigCapabilitiesDao(ses).findCapabilites(caps);
    assertNotNull(capsRec);
    assertEquals(caps, capsRec.getCapabilities());

    /* Test type record was created and added. */
    RigType typeRec = new RigTypeDao(ses).findByName(type);
    assertNotNull(typeRec);
    assertEquals(type, typeRec.getName());
    assertEquals(180, typeRec.getLogoffGraceDuration());
    assertFalse(typeRec.isCodeAssignable());

    /* Test rig record was created and added. */
    Rig rigRec = new RigDao(ses).findByName(name);
    assertNotNull(rigRec);
    assertNotNull(rigRec.getId());
    assertEquals(name, rigRec.getName());
    assertEquals(type, rigRec.getRigType().getName());
    assertTrue(rigRec.isActive());
    assertEquals(contactUrl, rigRec.getContactUrl());
    assertFalse(rigRec.isInSession());
    assertNotNull(rigRec.getLastUpdateTimestamp());
    assertTrue(rigRec.isManaged());
    assertNull(rigRec.getMeta());
    assertNotNull(rigRec.getOfflineReason());
    assertFalse(rigRec.isOnline());
    assertEquals(caps, rigRec.getRigCapabilities().getCapabilities());

    assertTrue(new Date(System.currentTimeMillis()).after(rigRec.getLastUpdateTimestamp()));
    assertTrue(new Date(System.currentTimeMillis() - 10000).before(rigRec.getLastUpdateTimestamp()));

    /* Cleanup. */
    ses.beginTransaction();
    ses.createQuery("DELETE FROM RigLog WHERE rig='" + rig.getId() + "'").executeUpdate();
    ses.getTransaction().commit();

    ses.beginTransaction();
    ses.delete(rigRec);
    ses.delete(capsRec);
    ses.delete(typeRec);
    ses.getTransaction().commit();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.rigprovider.impl.tests.RegisterLocalRigTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.rigprovider.impl.RegisterLocalRig#registerRig(java.lang.String, java.lang.String, java.lang.String, java.lang.String)}.
 */// w  w  w. j av  a 2 s.  co m
@Test
public void testAddRigToSchedServerActiveRig() {
    Session ses = DataAccessActivator.getNewSession();

    String name = "register1";
    String type = "registertest";
    String caps = "a,b,c,d,e,f";
    String contactUrl = "http://lbremote1.eng.uts.edu.au:7070/services/RigClientService";

    RigType existType = new RigType(type, 180, false);
    RigCapabilities existCaps = new RigCapabilities(caps);
    ses.beginTransaction();
    ses.save(existType);
    ses.save(existCaps);
    ses.getTransaction().commit();
    ses.flush();
    ses.evict(existType);
    ses.evict(existCaps);

    Rig existRig = new Rig(existType, existCaps, name, contactUrl, new Date(), false, "Broken.", false, true,
            true);
    ses.beginTransaction();
    ses.save(existRig);
    ses.getTransaction().commit();
    ses.flush();
    ses.evict(existRig);

    assertFalse(this.register.registerRig(name, type, caps, contactUrl));

    RigCapabilities capsRec = new RigCapabilitiesDao(ses).findCapabilites(caps);
    RigType typeRec = new RigTypeDao(ses).findByName(type);
    Rig rigRec = new RigDao(ses).findByName(name);

    /* Cleanup. */
    ses.beginTransaction();
    ses.createQuery("DELETE FROM RigLog WHERE rig='" + rigRec.getId() + "'").executeUpdate();
    ses.getTransaction().commit();

    ses.beginTransaction();
    ses.delete(rigRec);
    ses.delete(capsRec);
    ses.delete(typeRec);
    ses.getTransaction().commit();

    assertEquals("Exists", this.register.getFailedReason());
}

From source file:au.edu.uts.eng.remotelabs.schedserver.rigprovider.impl.tests.RemoveLocalRigTester.java

License:Open Source License

@Test
public void testRemoveRig() {
    Session ses = DataAccessActivator.getNewSession();
    RigType type = new RigType("rlrType", 180, false);
    new RigTypeDao(ses).persist(type);
    RigCapabilities caps = new RigCapabilities("rlrA,rlrB");
    new RigCapabilitiesDao(ses).persist(caps);
    Rig rig = new Rig(type, caps, "rlr", "http://rlr", new Date(System.currentTimeMillis() - 86400001), // A day ago
            true, null, false, false, true);
    rig.setActive(true);/*  w  w  w .j a  v a 2  s .  c om*/
    rig.setOnline(true);
    rig.setOfflineReason(null);
    new RigDao(ses).persist(rig);

    assertTrue(this.remove.removeRig("rlr", "Shutting down."));

    ses.refresh(rig);
    assertFalse(rig.isActive());
    assertFalse(rig.isOnline());
    assertNull(rig.getContactUrl());
    assertNotNull(rig.getOfflineReason());
    assertEquals("Shutting down.", rig.getOfflineReason());

    Date ts = rig.getLastUpdateTimestamp();
    assertNotNull(ts);
    assertTrue(new Date().after(ts));
    assertTrue(new Date(System.currentTimeMillis() - 5000).before(ts));

    ses.beginTransaction();
    ses.createQuery("DELETE FROM RigLog WHERE rig='" + rig.getId() + "'").executeUpdate();
    ses.getTransaction().commit();

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