Example usage for org.hibernate Query list

List of usage examples for org.hibernate Query list

Introduction

In this page you can find the example usage for org.hibernate Query list.

Prototype

List<R> list();

Source Link

Document

Return the query results as a List.

Usage

From source file:au.com.nicta.ct.db.CtKeyValueProperties.java

License:Open Source License

public static List findAllWithSuffix(String keySuffix) {

    List results = null;//from  w  ww.  jav a 2s  .  co  m
    Session s = CtSession.Create();

    try {
        s.beginTransaction();

        Query q = queryLike("%" + keySuffix, s);
        results = q.list();

        s.getTransaction().commit();
    } catch (Exception e) {
    } finally {
        s.close();
    }

    return results;
}

From source file:au.com.nicta.ct.db.CtKeyValueProperties.java

License:Open Source License

public static List findAllWithPrefix(String keyPrefix) {

    List results = null;//from  w  w  w  .j  av  a2  s  . c om
    Session s = CtSession.Create();

    try {
        s.beginTransaction();

        Query q = queryLike(keyPrefix + "%", s);
        results = q.list();

        s.getTransaction().commit();
    } catch (Exception e) {
    } finally {
        s.close();
    }

    return results;
}

From source file:au.com.nicta.ct.db.CtSession.java

License:Open Source License

public static List getObjects(Session s, String hql) {
    s.beginTransaction();/*from   www. ja va2s.  co m*/
    Query q = s.createQuery(hql);
    List results = q.list();

    s.getTransaction().commit();

    return results;
}

From source file:au.com.nicta.ct.db.entities.CtEntityPropertiesTypesUtil.java

License:Open Source License

public static List<CtEntityPropertiesTypes> find(Session s, Class entityName, String propertyName) {

    String qs = " SELECT ctEPT" + " FROM CtEntityPropertiesTypes as ctEPT" + " WHERE";

    String prefix = "";
    if (entityName != null) {
        qs += prefix + " ctEPT.entityName = :entityName";
        prefix = " AND";
    }//from w  w w .  jav a2  s.  co  m
    if (propertyName != null) {
        qs += prefix + " ctEPT.name = :name";
        prefix = " AND";
    }

    Query q = s.createQuery(qs);

    if (entityName != null) {
        q.setString("entityName", CtEntityPropertiesUtil.getClassName(entityName));
    }
    if (propertyName != null) {
        q.setString("name", propertyName);
    }

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

    return l;
}

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 .  j  a  v a  2  s  .c  o m*/

    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. ja v a2s. com
    }

    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();//www .ja v a2s  . c o 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.experiment.coordinates.CtLimits.java

License:Open Source License

protected void insertLimits(CtExperiments e) {

    String sql = " SELECT c.fk_coordinate_type, MIN( c.value ) as limit1, MAX( c.value ) as limit2 "
            + " FROM ct_images_coordinates ic "
            + " INNER JOIN ct_coordinates c ON ic.fk_coordinate = c.pk_coordinate"
            + " INNER JOIN ct_images i ON ic.fk_image = i.pk_image" + " WHERE fk_experiment = "
            + e.getPkExperiment() //--- for efficiency only update specific expt
            + " GROUP BY i.fk_experiment, fk_coordinate_type" + " ORDER BY i.fk_experiment, fk_coordinate_type";

    Session session = CtSession.Current();

    // 2 queries: first is a native one which is tuned to efficiently get
    // the PKs of the sequence, in order..
    // Sorry - 1 + 3n queries where n is number of dimensions...
    Query query = session.createSQLQuery(sql);

    Iterator i = query.list().iterator();

    while (i.hasNext()) {

        Object[] o = (Object[]) i.next();

        int fkCoordinateType = ((Integer) o[0]).intValue();
        int limit1 = ((Integer) o[1]).intValue();
        int limit2 = ((Integer) o[2]).intValue();

        sql = " SELECT min( pk_coordinate ), value " + " FROM ct_images_coordinates ic "
                + " INNER JOIN ct_coordinates c ON ic.fk_coordinate = c.pk_coordinate "
                + " INNER JOIN ct_images i on ic.fk_image = i.pk_image" + " WHERE fk_experiment = "
                + e.getPkExperiment()// --- for efficiency only update specific expt
                + " AND c.fk_coordinate_type = " + fkCoordinateType + " AND (c.value = " + limit1
                + " OR c.value = " + limit2 + " ) " + " GROUP BY c.value " + " ORDER BY c.value ";

        Query query2 = session.createSQLQuery(sql);

        Iterator i2 = query2.list().iterator();

        int pkCoordinate1 = -1;
        int pkCoordinate2 = -1;

        // should be 2 rows:
        int row = 0;

        while (i2.hasNext()) {
            Object[] o2 = (Object[]) i2.next();

            int pkCoordinate = ((Integer) o2[0]).intValue();

            if (row == 0) {
                pkCoordinate1 = pkCoordinate;
            } else {
                pkCoordinate2 = pkCoordinate;
            }//from  w  ww  . jav  a 2s  .  c  om

            ++row;
        }

        CtCoordinates c1 = (CtCoordinates) session.get(CtCoordinates.class, pkCoordinate1);
        CtCoordinates c2 = null;

        if (limit1 == limit2) {
            c2 = c1;
        } else {
            c2 = (CtCoordinates) session.get(CtCoordinates.class, pkCoordinate2);
        }

        CtExperimentsAxes ea = new CtExperimentsAxes();

        ea.setCtExperiments(e);
        ea.setCtCoordinatesByFkCoordinate1(c1);
        ea.setCtCoordinatesByFkCoordinate2(c2);

        Transaction t = session.beginTransaction();
        session.save(ea);
        //            session.persist( ea );
        t.commit(); //you might even want to wrap this in another try/catch block.
        //            session.delete( ea );
        //            session.save( ea );
        //            session.getTransaction().commit();
    }

    //        session.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();//w  w  w  . jav a2 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();//from   w  w w.j a v a2  s .  c om
    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;
}