Example usage for org.hibernate Criteria setResultTransformer

List of usage examples for org.hibernate Criteria setResultTransformer

Introduction

In this page you can find the example usage for org.hibernate Criteria setResultTransformer.

Prototype

public Criteria setResultTransformer(ResultTransformer resultTransformer);

Source Link

Document

Set a strategy for handling the query results.

Usage

From source file:com.gisgraphy.hibernate.projection.SpatialProjectionTest.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test/*w ww.j a  v  a  2s.  c  o m*/
@Ignore
public void testDistance() {
    float x1 = -2f;
    float y1 = 2f;
    float x2 = 2f;
    float y2 = 2f;
    Point locationParis = GeolocHelper.createPoint(x1, y1);
    //locationParis.setSRID(-1);
    Point locationbordeaux = GeolocHelper.createPoint(x2, y2);
    //locationbordeaux.setSRID(-1);

    final City p1 = GisgraphyTestHelper.createCity("paris", 0F, 0F, 1L);
    p1.setLocation(locationParis);
    City p2 = GisgraphyTestHelper.createCity("bordeaux", 0F, 0F, 3L);
    p2.setLocation(locationbordeaux);

    this.cityDao.save(p1);
    this.cityDao.save(p2);

    HibernateCallback hibernateCallback = new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {

            Criteria testCriteria = session.createCriteria(City.class);
            ProjectionList projection = Projections.projectionList()
                    .add(Projections.property("name").as("name")).add(SpatialProjection
                            .distance(p1.getLocation(), GisFeature.LOCATION_COLUMN_NAME).as("distance"));
            // remove the from point
            testCriteria.add(Restrictions.ne("id", p1.getId())).setProjection(projection);
            testCriteria.setResultTransformer(Transformers.aliasToBean(_CityDTO.class));

            List<_CityDTO> results = testCriteria.list();
            return results;
        }
    };

    List<_CityDTO> cities = (List<_CityDTO>) testDao.testCallback(hibernateCallback);
    assertEquals(1, cities.size());
    assertEquals("bordeaux", cities.get(0).getName());
    Double calculatedDist = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));//cartesian distance
    Double retrieveDistance = cities.get(0).getDistance();
    double percent = (Math.abs(calculatedDist - retrieveDistance) * 100)
            / Math.min(retrieveDistance, calculatedDist);
    assertTrue("There is more than one percent of error beetween the calculated distance (" + calculatedDist
            + ") and the retrieved one (" + retrieveDistance + ")", percent < 1);
}

From source file:com.googlecode.encomendaz.persistence.implementors.RastreioDAOImpl.java

License:Open Source License

@Override
public List<Rastreio> obterRastreiosAtivos() throws DAOException {
    Criteria criteria = HibernateUtil.getSessionFactory().openSession().createCriteria(Rastreio.class);
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    criteria.add(Restrictions.eq("entregue", false)).createCriteria("situacoes").addOrder(Order.desc("data"));
    return criteria.list();
}

From source file:com.googlecode.encomendaz.persistence.implementors.RastreioDAOImpl.java

License:Open Source License

@Override
public List<Rastreio> obterRastreiosInativos() throws DAOException {
    Criteria criteria = HibernateUtil.getSessionFactory().openSession().createCriteria(Rastreio.class);
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    criteria.add(Restrictions.eq("entregue", true)).createCriteria("situacoes").addOrder(Order.desc("data"));
    return criteria.list();
}

From source file:com.googlecode.encomendaz.persistence.implementors.RastreioDAOImpl.java

License:Open Source License

@Override
public List<Rastreio> obterTodosRastreios() throws DAOException {
    Criteria criteria = HibernateUtil.getSessionFactory().openSession().createCriteria(Rastreio.class);
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    criteria.createCriteria("situacoes").addOrder(Order.desc("data"));
    return criteria.list();

}

From source file:com.gotour.daos.GenericDaoImpl.java

@Override
public List<T> getAll() {
    Session session = sessionFactory.getCurrentSession();
    Criteria crit = session.createCriteria(type);
    crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return crit.list();
}

From source file:com.gotour.daos.TourDaoImpl.java

public List<Tour> getTours(City city, Theme theme) {
    Criteria crit = getSession().createCriteria(getType());
    crit.add(Restrictions.eq("city", city));
    crit.add(Restrictions.eq("theme", theme));
    crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return crit.list();
}

From source file:com.gp.cong.logisoft.hibernate.dao.SedSchedulebDetailsDAO.java

public List findByDr(String fileNo, String trnRef) throws Exception {
    Criteria criteria = getCurrentSession().createCriteria(SedSchedulebDetails.class);
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    if (CommonUtils.isNotEmpty(trnRef)) {
        criteria.add(Restrictions.eq("trnref", trnRef));
        criteria.addOrder(Order.asc("trnref"));
        return criteria.list();
    }//www .j  av  a 2s  .  c o  m
    return null;
}

From source file:com.grand.ids.jdbc.knowledgemodule.id.IdKnowledgeModule.java

License:Apache License

protected Graph getGraph(final Table table, ResultSet resultSet, final UserId userId) throws Exception {
    final List<List<Object>> ids = getObjectIds(resultSet, table);
    final Graph graph = new Graph(ids.size());
    doInTransaction(new Action() {

        public void doAction(Session session) {
            Set<String> idSet = new HashSet<String>();
            Map<String, Integer> nodesNum = new HashMap<String, Integer>();
            int num = 0;
            for (List<Object> id : ids) {
                String stringId = getStringIdPresentation(id);
                idSet.add(stringId);//from w  w  w .  j ava 2  s.c om
                nodesNum.put(stringId, num++);
            }
            Criteria criteria = session.createCriteria(RecordInfo.class);
            criteria.add(Restrictions.in("recordId", idSet));
            criteria.add(Restrictions.eq("tableName", table.getName()));
            criteria.add(Restrictions.eq("userId", userId.getUserId()));
            criteria.setFetchMode("friendlyRecordIds", FetchMode.JOIN);
            criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
            List<RecordInfo> recordInfos = criteria.list();

            Set<String> edges = new HashSet<String>();
            for (RecordInfo recordInfoA : recordInfos) {
                Integer nodeId = nodesNum.get(recordInfoA.getRecordId());
                Set<String> friendIds = recordInfoA.getFriendlyRecordIds();
                for (RecordInfo recordInfoB : recordInfos) {
                    Integer friendNodeId = nodesNum.get(recordInfoB.getRecordId());
                    String edge = String.format("%d-%d", friendNodeId, nodeId);
                    if (!edges.contains(edge)) {
                        if (friendIds.contains(recordInfoB.getRecordId())) {
                            graph.addEdge(nodeId, friendNodeId, 1.0);
                        } else {
                            graph.addEdge(nodeId, friendNodeId,
                                    getLinkPredictionScore(table, userId, session, recordInfoA, recordInfoB));
                        }
                        edges.add(String.format("%d-%d", nodeId, friendNodeId));
                    }
                }
            }
        }
    });
    return graph;
}

From source file:com.grand.ids.jdbc.knowledgemodule.id.IdKnowledgeModule.java

License:Apache License

private double getLinkPredictionScore(Table table, UserId userId, Session session, RecordInfo recordInfoA,
        RecordInfo recordInfoB) {// w  ww .  ja  v a  2s .c o m
    if (linkPredictionMode == LinkPredictionMode.ADAMIC_ADAR) {
        Set<String> intersection = new HashSet<String>(recordInfoA.getFriendlyRecordIds());
        intersection.retainAll(recordInfoB.getFriendlyRecordIds());
        if (intersection.size() > 0) {
            Criteria criteria = session.createCriteria(RecordInfo.class);
            criteria.add(Restrictions.in("recordId", intersection));
            criteria.add(Restrictions.eq("tableName", table.getName()));
            criteria.add(Restrictions.eq("userId", userId.getUserId()));
            criteria.setFetchMode("friendlyRecordIds", FetchMode.JOIN);
            criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
            List<RecordInfo> recordInfos = criteria.list();
            double sum = 0;
            for (RecordInfo recordInfo : recordInfos) {
                sum += Math.log(2) / Math.log(recordInfo.getFriendlyRecordIds().size());
            }
            double score = sum / intersection.size();
            if ((score > 1.0) || (score < 0.0)) {
                throw new RuntimeException("Error!");
            }
            return score;
        } else {
            return 0.0;
        }
    } else if (linkPredictionMode == LinkPredictionMode.JACCARD) {
        Set<String> friendsA = recordInfoA.getFriendlyRecordIds();
        Set<String> friendsB = recordInfoB.getFriendlyRecordIds();
        Set<String> union = new HashSet<String>(friendsA);
        union.addAll(friendsB);
        Set<String> intersection = new HashSet<String>(friendsA);
        intersection.retainAll(friendsB);
        System.out.println(intersection.size() + " " + union.size() + " : "
                + ((union.size() > 0) ? (double) intersection.size() / union.size() : 0.0));
        return (union.size() > 0) ? ((double) intersection.size()) / union.size() : 0.0;
    }
    return 0.0;
}

From source file:com.grand.ids.jdbc.knowledgemodule.id.IdKnowledgeModule.java

License:Apache License

private void updateRecordInfos(final String tableName, final List<List<Object>> ids, final UserId userId) {
    doInTransaction(new Action() {

        public void doAction(Session session) {
            Set<String> idSet = new HashSet<String>();
            for (List<Object> id : ids) {
                idSet.add(getStringIdPresentation(id));
            }//w  w w .  j  ava2  s .c  o  m
            Criteria criteria = session.createCriteria(RecordInfo.class);
            criteria.add(Restrictions.in("recordId", idSet));
            criteria.add(Restrictions.eq("tableName", tableName));
            criteria.add(Restrictions.eq("userId", userId.getUserId()));
            criteria.setFetchMode("friendlyRecordIds", FetchMode.JOIN);
            criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
            List<RecordInfo> recordInfos = criteria.list();
            Set<String> nonExistIds = new HashSet<String>(idSet);
            for (RecordInfo recordInfo : recordInfos) {
                recordInfo.getFriendlyRecordIds().addAll(idSet);
                recordInfo.setHits(recordInfo.getHits() + 1);
                session.update(recordInfo);
                nonExistIds.remove(recordInfo.getRecordId());
            }
            for (String id : nonExistIds) {
                RecordInfo recordInfo = new RecordInfo();
                recordInfo.setRecordId(id);
                recordInfo.setTableName(tableName);
                recordInfo.setFriendlyRecordIds(idSet);
                recordInfo.setHits(1L);
                recordInfo.setUserId(userId.getUserId());
                session.save(recordInfo);
            }
        }
    });
}