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.db.entities.CtEntityPropertiesUtil.java

License:Open Source License

public static List<CtEntityProperties> find(Session s, Integer solutionPk, Class entityName, Integer entityPk,
        String propertyName) {/*from   w  w w  .  ja  v  a 2s  . c om*/

    String qs = " SELECT ctEP" + " FROM CtEntityProperties as ctEP" + " WHERE";

    String prefix = "";
    if (solutionPk != null) {
        qs += prefix + " ctEP.ctSolutions = :ctSolutions";
        prefix = " AND";
    }
    if (entityName != null) {
        qs += prefix + " ctEP.entityName = :entityName";
        prefix = " AND";
    }
    if (entityPk != null) {
        qs += prefix + " ctEP.entityPk = :entityPk";
        prefix = " AND";
    }
    if (propertyName != null) {
        qs += prefix + " ctEP.name = :name";
        prefix = " AND";
    }

    Query q = s.createQuery(qs);

    if (solutionPk != null) {
        q.setInteger("ctSolutions", solutionPk);
        //            System.out.println( solutionPk );
    }
    if (entityName != null) {
        q.setString("entityName", getClassName(entityName));
        //            System.out.println( getClassName(entityName) );
    }
    if (entityPk != null) {
        q.setInteger("entityPk", entityPk);
        //            System.out.println( entityPk );
    }
    if (propertyName != null) {
        q.setString("name", propertyName);
        //            System.out.println( propertyName );
    }
    List<CtEntityProperties> l = (List<CtEntityProperties>) q.list();

    //        long end = System.nanoTime();
    //        System.out.println( "Time(ms): " + (end-sta)/1000000.0 );

    return l;
}

From source file:au.com.nicta.ct.experiment.coordinates.CtAxesModel.java

License:Open Source License

public void createAxes(String query) {

    if (query == null) {
        return;//from  w  w  w.  j  a v  a  2  s  .c om
    }

    Session s = CtSession.Current();
    s.beginTransaction();

    Query q = s.createQuery(query);

    List results = q.list();

    createAxes(results);

    s.getTransaction().commit();
}

From source file:au.com.nicta.ct.experiment.coordinates.CtCoordinatesModel.java

License:Open Source License

public void createImages() {
    _cachedImages.clear();/*from ww w. j a v  a  2s . co m*/

    HashSet<CtImages> completed = new HashSet<CtImages>();

    Session s = CtSession.Current();
    s.beginTransaction();

    String hql = "FROM CtImages i " + "INNER JOIN FETCH i.ctImagesCoordinateses ic "
            + "INNER JOIN FETCH ic.ctCoordinates c " // put into hibernate cache, ie i obj should be complete
            + "INNER JOIN FETCH c.ctCoordinatesTypes ct " + "WHERE i.ctExperiments = :experiment "
            + "ORDER BY i, ct";

    Query q = s.createQuery(hql);

    q.setParameter("experiment", _am._e);
    List<Object[]> l = (List<Object[]>) q.list(); // all data should be accessed once, here.

    Iterator i = l.iterator();

    while (i.hasNext()) {

        CtImages image = (CtImages) i.next();
        //            Object[] os = (Object[])i.next();
        //
        //            CtImages image = (CtImages)os[ 0 ];

        if (completed.contains(image)) {
            continue; // already loaded this one.
        }

        ArrayList<CtAbstractPair<String, Integer>> al = new ArrayList<CtAbstractPair<String, Integer>>();

        // hibernate should've loaded the fetches objects, so:
        //            CtImages image = (CtImages)           os[ 0 ];
        //            CtImagesCoordinates ic = (CtImagesCoordinates)os[ 1 ];
        //            CtCoordinates       c  = (CtCoordinates)      os[ 2 ];
        //            CtCoordinatesTypes  ct = (CtCoordinatesTypes) os[ 3 ];

        // each image has multiple coordinates/types. build a list of them all.
        Set<CtImagesCoordinates> sic = image.getCtImagesCoordinateses(); // TODO: Check this is auto preloaded and not generating further DB hits..

        for (CtImagesCoordinates ic : sic) {
            CtCoordinates c = ic.getCtCoordinates();
            CtCoordinatesTypes ct = c.getCtCoordinatesTypes();

            CtAbstractPair<String, Integer> ap = new CtAbstractPair<String, Integer>();

            ap._first = ct.getName();
            ap._second = c.getValue();

            al.add(ap);
        }

        String key = getImageKey(al);

        _cachedImages.put(key, image);
        completed.add(image);
    }

    s.getTransaction().commit();
}

From source file:au.com.nicta.ct.solution.export.concrete.CtAnnotationsExportProcess.java

License:Open Source License

public String apply(CtSolutions sol, String filePath) {

    ecsv.clear();//from w w w . j  a  v  a 2 s  .  c  o m

    // make list of events, ordered by time.
    ecsv.addHeader(getFields());

    Session s = CtSession.Current();
    CtManualFlush mf = new CtManualFlush(s);

    Query q = s
            .createQuery(" SELECT ctA" + " FROM CtAnnotations as ctA" + " WHERE ctA.ctSolutions = :solution");

    q.setInteger("solution", sol.getPkSolution());

    List<CtAnnotations> l = (List<CtAnnotations>) q.list();
    for (CtAnnotations a : l) {
        ArrayList<String> fields = new ArrayList<String>();

        fields.add(String.valueOf(a.getPkAnnotation()));
        fields.add(String.valueOf(a.getCtAnnotationsTypes().getValue()));
        fields.add(String.valueOf(a.getCtSolutions().getPkSolution()));
        fields.add(String.valueOf(a.getCtImages().getPkImage()));
        fields.add(String.valueOf(a.getCtImages().getUri()));
        fields.add(String.valueOf(a.getValue()));
        fields.add(String.valueOf(a.getX()));
        fields.add(String.valueOf(a.getY()));

        ecsv.addRow(fields);
    }

    mf.restore();

    // save events to file..
    String result = ecsv.write(filePath);
    return result;
}

From source file:au.com.nicta.ct.solution.export.concrete.CtImagesCoordinatesExportProcess.java

License:Open Source License

public String apply(CtSolutions sol, String filePath) {

    ecsv.clear();/*w  ww .j  a v a2 s  .c  o m*/
    ecsv.addHeader(getFields());

    Session s = CtSession.Current();
    CtManualFlush mf = new CtManualFlush(s);

    Query q = s.createQuery(" SELECT ctIC" + " FROM CtImagesCoordinates as ctIC");

    List<CtImagesCoordinates> l = (List<CtImagesCoordinates>) q.list();

    int currentExperiment = sol.getCtExperiments().getPkExperiment();
    for (CtImagesCoordinates ic : l) {
        // skip if not in current experiment
        if (ic.getCtImages().getCtExperiments().getPkExperiment() != currentExperiment) {
            continue;
        }

        ArrayList<String> fields = new ArrayList<String>();

        fields.add(String.valueOf(ic.getPkImageCoordinate()));
        fields.add(String.valueOf(ic.getCtImages().getPkImage()));
        fields.add(String.valueOf(ic.getCtImages().getUri()));
        fields.add(String.valueOf(ic.getCtCoordinates().getCtCoordinatesTypes().getName()));
        fields.add(String.valueOf(ic.getCtCoordinates().getValue()));
        fields.add(String.valueOf(ic.getCtCoordinates().getName()));

        ecsv.addRow(fields);
    }

    mf.restore();

    String result = ecsv.write(filePath);
    return result;
}

From source file:au.com.nicta.ct.solution.graphics.canvas.tools.annotations.CtAnnotationsController.java

License:Open Source License

final List<CtAnnotationsTypes> loadTypes() {
    Session s = CtSession.Current();
    CtManualFlush mf = new CtManualFlush(s);

    s.beginTransaction();/*  ww w. j  a  v a  2  s.c o m*/

    Query q = s.createQuery(" SELECT at" + " FROM CtAnnotationsTypes at");

    List<CtAnnotationsTypes> l = (List<CtAnnotationsTypes>) q.list();

    s.getTransaction().commit();
    mf.restore();

    return l;
}

From source file:au.com.nicta.ct.solution.graphics.canvas.tools.annotations.CtAnnotationsModel.java

License:Open Source License

public void setSolution(CtSolutions s) {

    if (s == null) {
        clear();/*  w ww  . j a v a 2 s.c o m*/
        return;
    }

    Session session = CtSession.Current();
    session.beginTransaction();

    Query q = session
            .createQuery(" SELECT ctA" + " FROM CtAnnotations as ctA" + " WHERE ctA.ctSolutions = :solutionPk");
    q.setInteger("solutionPk", s.getPkSolution());

    for (CtAnnotations a : (List<CtAnnotations>) q.list()) {
        addToMaps(a);
    }

    session.getTransaction().commit();
}

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

License:Open Source License

protected void deleteTracks(boolean showProgress) { // i.e. all tracks

    if (!valid())
        return;// ww w . j a  v  a 2  s  .  c  o  m

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

    //        String hql1 = " DELETE FROM CtTracksDetections td "
    //                    + " WHERE td.ctTracks IN ( "
    //                    + " SELECT t FROM CtTracks t "
    //                    + " INNER JOIN t.ctSolutions s "
    //                    + " WHERE s.pkSolution = " + solution.getPkSolution() +" ) ";

    String hql1 = " SELECT td FROM CtTracksDetections td " + " WHERE td.ctTracks IN ( "
            + " SELECT t FROM CtTracks t " + " INNER JOIN t.ctSolutions s " + " WHERE s.pkSolution = "
            + _s.getPkSolution() + " ) ";

    //        String hql1 = " SELECT t FROM CtTracks t "
    //                    + " INNER JOIN t.ctSolutions s "
    //                    + " WHERE s.pkSolution = " + solution.getPkSolution();// +" ) ";
    String hql2 = " DELETE FROM CtTracksDetections td " + " WHERE td IN (:vals) ";

    String hql3 = " DELETE FROM CtTracks t " + " WHERE t.ctSolutions = " + _s.getPkSolution();

    //        clear();

    Session session = CtSession.Current();
    session.beginTransaction();

    Query q1 = session.createQuery(hql1);
    List results = q1.list();

    if (results.isEmpty()) {
        session.getTransaction().commit();
        return; // mnothing changed
    }

    Query q2 = session.createQuery(hql2);

    q2.setParameterList("vals", results);

    int rowCount2 = q2.executeUpdate(); // this query fails

    Query q3 = session.createQuery(hql3);
    int rowCount3 = q3.executeUpdate();

    session.getTransaction().commit();
    session.refresh(_s);

    ////////////////////////////////////////////////////////////////////////
    // Manually refresh detections and tracks as seems to be a bug with 
    // their consistency
    ////////////////////////////////////////////////////////////////////////
    CtPageFrame.showWaitCursor();

    Set<CtDetections> cd = _s.getCtDetectionses();
    Set<CtTracks> ct = _s.getCtTrackses();

    for (CtDetections d : cd) {
        session.refresh(d);
    }

    for (CtTracks t : ct) {
        session.refresh(t);
    }

    CtPageFrame.showDefaultCursor();
    ////////////////////////////////////////////////////////////////////////

    refresh(_cc, _ism, _twm, _s, showProgress);
    //        fireModelChanged(); in update anyway
}

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

License:Open Source License

static void dropAllExistingTracks(Session session) {
    // Clear the current solution of all tracks
    String hql = "SELECT ctTracksDetections" + " FROM CtTracksDetections as ctTracksDetections"
            + " JOIN ctTracksDetections.ctTracks ctTracks" + " WHERE ctTracks.ctSolutions = :solutionPk";

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

    for (CtTracksDetections td : results) {
        session.delete(td);//from   ww  w  .j a  va  2 s  .  co m
    }

    hql = "SELECT ctTracks" + " FROM CtTracks as ctTracks" + " WHERE ctTracks.ctSolutions = :solutionPk";

    q = session.createQuery(hql);
    q.setInteger("solutionPk", CtSolutionController.getSolutions().getPkSolution());
    List<CtTracks> results2 = q.list();

    for (CtTracks t : results2) {
        session.delete(t);
    }

}

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

License:Open Source License

static void dropExistingTracks(Session session, TkTracks tracks, int startTimeIdxInclusive,
        int endTimeIdxInclusive) {

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

    // Drop all existing track detection associations
    int trackCnt = 0;
    for (TkTrack t : tracks) {
        ++trackCnt;/*w w w. j  a v a2  s  . co m*/

        for (TkTrackElement e : t.elements) {
            TkDetection d = e.det;
            if (d == null) {
                continue;
            }

            System.out.println("d.timeIdx: " + (d.timeIdx));
            System.out.println("trackCnt: " + (trackCnt));
            System.out.println("total no. tracks: " + (trackCnt));
            if (d.timeIdx == startTimeIdxInclusive || d.timeIdx == endTimeIdxInclusive) {
                continue;
            }

            Integer detectionPk = d.find(DETECTION_PK);

            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<CtTracksDetections> results = q.list();

            HashSet<CtTracks> modified = new HashSet<CtTracks>();

            for (CtTracksDetections td : results) {
                CtTracks t_n = td.getCtTracks();
                CtDetections d_n = td.getCtDetections();
                CtSolutions s = t_n.getCtSolutions();

                if (s.getPkSolution() == solution.getPkSolution()) {
                    //                    if( td.getCtTracks().getCtSolutions().getPkSolution() == solution.getPkSolution() ) {
                    t_n.getCtTracksDetectionses().remove(td);
                    d_n.getCtTracksDetectionses().remove(td);
                    session.delete(td);
                    modified.add(t_n);
                }
            }

            // now clean up tracks that have no surviving detections:
            for (CtTracks t_n : modified) {
                Set<CtTracksDetections> tds = t_n.getCtTracksDetectionses();

                if (tds.isEmpty()) {
                    CtSolutions s = t_n.getCtSolutions();
                    s.getCtTrackses().remove(t_n);
                    session.delete(t);
                    session.update(s);
                }
            }

            //                String hql =
            //                      "DELETE from CtTracksDetections as ctTracksDetections"
            //                    + " WHERE fk_detection = " + detectionPk;
            //                System.out.println("hql: " + (hql) );
            //
            //                Query query = session.createQuery(hql);
            //                int row = query.executeUpdate();
            //                System.out.println("Rows deleted: " + row);
        }
    }
}