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.com.nicta.ct.solution.tracking.jipda.CtTkDBTracker.java

License:Open Source License

static CtTracks findExistingTrackAtStart(Session session, TkTrack t, int startTimeIdxInclusive) {
    //        session.flush();

    TkDetection firstDetection = t.elements.get(0).det;
    if (firstDetection == null) {
        throw new Error("First detection of a track can not be null");
    }/*  www .j a  v  a 2s  .  co  m*/

    // join first detection to any existing tracks
    if (firstDetection.timeIdx != startTimeIdxInclusive) {
        return null;
    }

    CtSolutions solution = (CtSolutions) CtObjectDirectory.get("solution");

    Integer detectionPk = firstDetection.find(DETECTION_PK);

    // retrieve the track that contains the detection
    String hql = " SELECT ctTD" + " FROM CtTracksDetections as ctTD" + " JOIN ctTD.ctDetections as ctD"
            + " WHERE ctD.pkDetection = :detectionPk";
    System.out.println("hql: " + (hql));

    //        session.flush();
    Query q = session.createQuery(hql);
    q.setInteger("detectionPk", detectionPk);
    List<CtTracksDetections> results = q.list();

    // make sure there's only one track associated with this detection
    CtTracks track = null;
    for (CtTracksDetections td : results) {
        if (td.getCtTracks().getCtSolutions().getPkSolution() == solution.getPkSolution()) {
            if (track != null) {
                throw new Error("Detection associated with more than 1 track,"
                        + "this should not happen when all tracks-detections assoc"
                        + "have been cleaned between start and end time idx.");
            }

            track = td.getCtTracks(); // use existing one
        }
    }

    return track; // may be null if detection is not in a track
}

From source file:au.com.nicta.ct.solution.tracking.jipda.CtTkDBTracker.java

License:Open Source License

static boolean mergeTracksAtEnd(Session session, TkTrack t, int endTimeIdxInclusive, CtTracks tt) {
    //        session.flush();

    TkDetection lastDetection = t.getLast().det;

    if (lastDetection == null) {
        return false; // no detection associated at track's end.
    }//from  w w w.  j  a  v  a  2  s  .  com

    // join first detection to any existing tracks
    if (lastDetection.timeIdx != endTimeIdxInclusive) {
        return false; // no detection on the boundary
    }

    Integer detectionPk = lastDetection.find(DETECTION_PK);

    // retrieve all tracks that contains the detection
    // note that this last detection may be a place for a fork.
    String hql = "SELECT ctTracksDetections" + " FROM CtTracksDetections as ctTracksDetections"
            + " JOIN ctTracksDetections.ctDetections ctDetections"
            + " WHERE ctDetections.pkDetection = :detectionPk";

    Query q = session.createQuery(hql);
    q.setInteger("detectionPk", detectionPk);
    List results = q.list();

    if (results.isEmpty()) {
        return false; // no existing tracks so no need to remap
    }

    if (results.size() > 1) {
        // this is a forking detection. When gets here:
        // - all tracks-detection assoc before this detection has been cleared
        // And since this is a fork, the track does not continue past this
        // detection. Since this detection will be associated with the new track
        // we don't have to remap anything.
        return false;
    }

    CtTracksDetections td = (CtTracksDetections) results.get(0);

    Set<CtTracksDetections> remap = td.getCtTracks().getCtTracksDetectionses();

    for (CtTracksDetections td2 : remap) {
        System.out.println(
                "Remapping: " + td2.getCtTracks().getPkTrack() + " " + td2.getCtDetections().getPkDetection()
                        + " to: " + tt.getPkTrack() + " " + td2.getCtDetections().getPkDetection());

        if (td2.getCtTracks().getPkTrack() == tt.getPkTrack()) {
            continue; // no change
        }

        CtTracks tt0 = td2.getCtTracks();
        td2.setCtTracks(tt);
        tt0.getCtTracksDetectionses().remove(td2);
        tt.getCtTracksDetectionses().add(td2);
        session.update(td2);
        session.update(tt);
        session.update(tt0);
    }

    //        session.flush();
    return true;
}

From source file:au.com.nicta.ct.solution.tracking.jipda.CtTkDBTracker.java

License:Open Source License

static void removeEmptyTracks(Session session) {
    CtSolutions solution = (CtSolutions) CtObjectDirectory.get("solution");
    String hql = "SELECT ctTracks" + " FROM CtTracks as ctTracks" + " JOIN ctTracks.ctSolutions as ctSolutions"
            + " WHERE ctSolutions.pkSolution = :solutionPk";

    Query q = session.createQuery(hql);
    q.setInteger("solutionPk", solution.getPkSolution());
    List<CtTracks> results = q.list();

    for (CtTracks t : results) {
        Set<CtTracksDetections> s = t.getCtTracksDetectionses();
        //            System.out.println("t.getPkTrack(): " + (t.getPkTrack()) );
        //            System.out.println("t.getCtTracksDetectionses().size(): " + (t.getCtTracksDetectionses().size()) );
        //            System.out.println("s.isEmpty(): " + (s.isEmpty()) );;

        if (s.isEmpty()) {
            // empty track, delete it
            solution.getCtTrackses().remove(t);
            session.update(solution);//from   www  . j  av  a2  s .  com
            session.delete(t);
        }
    }
}

From source file:au.edu.anu.metadatastores.datamodel.aries.grants.ForCodesTest.java

License:Open Source License

/**
 * Queries for the fields of research in the database and prints them out
 *//* w  w w  .j a  v  a 2s .  c  om*/
@Test
public void test() {
    Session session = AriesHibernateUtil.getSessionFactory().openSession();

    try {
        Query query = session.createQuery("FROM ForCodes WHERE chrForObjectiveCode = :code");
        query.setParameter("code", "111706");

        ForCodes forSubject = (ForCodes) query.uniqueResult();

        assertNotNull("No Field of Research Found", forSubject);
        assertEquals("No subject description found", "Epidemiology", forSubject.getChrForDescription());
        assertEquals("No subject division code found", "11", forSubject.getChrForDivisionCode());
        assertEquals("No subject group code found", "1117", forSubject.getChrForGroupCode());
        assertEquals("No subject objective code found", "111706", forSubject.getChrForObjectiveCode());
    } finally {
        session.close();
    }
}

From source file:au.edu.anu.metadatastores.datamodel.aries.grants.ForCodesTest.java

License:Open Source License

@Test
public void test2() {
    Session session = AriesHibernateUtil.getSessionFactory().openSession();
    try {//from w  ww .  ja  va2s. c o m

        Query query = session.createQuery("FROM ForCodes");
        //   query.setParameter("code", "111706");

        List<ForCodes> forCodes = query.list();
        assertNotNull("No Fields of Research Found", forCodes);
        assertTrue("No field of research codes found", forCodes.size() > 0);
    } finally {
        session.close();
    }

}

From source file:au.edu.anu.metadatastores.harvester.Harvest.java

License:Open Source License

/**
 * Harvest the records from the given system
 * //from   w ww  . j a  v  a  2  s  .  co m
 * @param harvestSystem The string of the system to harvest records for
 * @throws HarvestException
 */
public void harvest(String harvestSystem) throws HarvestException {
    Session session = HarvesterHibernateUtil.getSessionFactory().openSession();
    Location location = null;
    try {
        Query query = session.createQuery("FROM Location WHERE system = :system");
        query.setParameter("system", harvestSystem);

        location = (Location) query.uniqueResult();
    } finally {
        session.close();
    }
    if (location == null) {
        harvest(location);
    } else {
        throw new HarvestException("No location found for the system: " + harvestSystem);
    }
}

From source file:au.edu.anu.metadatastores.harvester.HarvestContentService.java

License:Open Source License

/**
 * Get the next row of harvest content to process
 * //from  w w w  . java2  s.co m
 * @param system The system to get the harvest content for
 * @return The harvest content record
 */
public HarvestContent getNextHarvestContent(String system) {
    Session session = HarvesterHibernateUtil.getSessionFactory().openSession();
    try {
        Query query = session.createQuery("FROM HarvestContent WHERE system = :system ORDER BY hid");
        query.setParameter("system", system);

        HarvestContent content = (HarvestContent) query.setMaxResults(1).uniqueResult();

        return content;
    } finally {
        session.close();
    }
}

From source file:au.edu.anu.metadatastores.harvester.HarvestContentService.java

License:Open Source License

/**
 * Get the harvest locations/* w  ww  . j  a v  a2 s .  co m*/
 * 
 * @return The harvest locations
 */
public List<Location> getHarvestLocations() {
    Session session = HarvesterHibernateUtil.getSessionFactory().openSession();
    try {
        @SuppressWarnings("unchecked")
        List<Location> locations = session.createQuery("FROM Location").list();
        return locations;
    } finally {
        session.close();
    }
}

From source file:au.edu.anu.metadatastores.rdf.RDFService.java

License:Open Source License

/**
 * Get all the objects from the metadata stores database and reference them.
 *///from  ww w  . j a  v  a 2 s .  c o m
public void updateAll() {
    LOGGER.debug("In Update All");
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        //TODO remove the limit however for testing purposes the limit creates issues
        org.hibernate.Query query = session.createQuery("FROM Item");
        query.setMaxResults(10);
        @SuppressWarnings("unchecked")
        List<Item> items = query.list();
        update(items);
    } finally {
        session.close();
    }
}

From source file:au.edu.anu.metadatastores.rdf.RDFService.java

License:Open Source License

public void updateModifiedAfterDate(Date modifiedDate) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {//from  w ww  . j  a va2  s  .c  o m
        org.hibernate.Query query = session.createQuery(
                "SELECT item FROM Item item WHERE EXISTS ( SELECT 1 FROM item.itemAttributes ia WHERE ia.lastModified > :lastModifiedDate ) ");
        query.setParameter("lastModifiedDate", modifiedDate);

        @SuppressWarnings("unchecked")
        List<Item> items = query.list();
        update(items);
    } finally {
        session.close();
    }
}