List of usage examples for org.hibernate Criteria setResultTransformer
public Criteria setResultTransformer(ResultTransformer resultTransformer);
From source file:com.ewabackend.dao.ResultDAOImplementation.java
@Override public List<Result> findAllResults() { Criteria criteria = createEntityCriteria().addOrder(Order.asc("id")); criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); List<Result> results = (List<Result>) criteria.list(); return results; }
From source file:com.ewabackend.dao.SubjectDAOImplementation.java
@Override public List<Subject> findAllSubjects() { Criteria criteria = createEntityCriteria().addOrder(Order.asc("id")); criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); List<Subject> subjects = (List<Subject>) criteria.list(); return subjects; }
From source file:com.ewabackend.dao.SubjectPartDAOImplementation.java
@Override public List<SubjectPart> findAllSubjectParts() { Criteria criteria = createEntityCriteria().addOrder(Order.asc("id")); criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); List<SubjectPart> subjects = (List<SubjectPart>) criteria.list(); return subjects; }
From source file:com.fich.wafproject.dao.ConfigurationFileAttributeDaoImpl.java
public List<ConfigurationFilesAttributes> findByFileConfiguration(Long id) { Criteria crit = this.createEntityCriteria(); crit.createAlias("configurationFileAttributeGroups", "cfag") .add(Restrictions.eq("cfag.configurationFiles.id", id)); return (List<ConfigurationFilesAttributes>) crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list(); }
From source file:com.fich.wafproject.dao.ConfigurationFileDaoImpl.java
public ConfigurationFiles findById(Long id) { Criteria criteria = this.createEntityCriteria(); criteria.add(Restrictions.eq("id", id)); // .setFetchMode("configurationFileAttributeGroups", FetchMode.JOIN); return (ConfigurationFiles) criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).uniqueResult(); }
From source file:com.gettec.fsnip.fsn.dao.product.impl.ProductDAOImpl.java
@Override public Product checkProduct(Product product) throws ServiceException { Criteria criteria = getSession().createCriteria(Product.class); BusinessUnit businessUnit = product.getProducer(); if (null != businessUnit && !StringUtil.isBlank(businessUnit.getName())) { criteria.createAlias("producer", "producer"); criteria.add(Restrictions.eq("producer.name", businessUnit.getName())); }/*from ww w .j a va2s . c om*/ if (!StringUtil.isBlank(product.getName())) { criteria.add(Restrictions.eq("name", product.getName())); } if (!StringUtil.isBlank(product.getFormat())) { criteria.add(Restrictions.eq("format", product.getFormat())); } criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);// ROOT_ENTITY if (criteria.list().size() > 0) { return (Product) criteria.list().get(0); } return null; }
From source file:com.gisgraphy.hibernate.projection.ProjectionBeanTest.java
License:Open Source License
@SuppressWarnings("unchecked") @Test//from w ww. j ava2s .com public void testPropertiesList() { City p1 = GisgraphyTestHelper.createCity("paris", 48.86667F, 2.3333F, 1L); this.cityDao.save(p1); HibernateCallback hibernateCallback = new HibernateCallback() { public Object doInHibernate(Session session) throws PersistenceException { Criteria testCriteria = session.createCriteria(City.class); List<String> fieldList = new ArrayList<String>(); fieldList.add("name"); fieldList.add("featureId"); ProjectionList projection = ProjectionBean.fieldList(fieldList, true); testCriteria.setProjection(projection); testCriteria.setResultTransformer(Transformers.aliasToBean(City.class)); List<City> results = testCriteria.list(); return results; } }; List<City> cities = (List<City>) testDao.testCallback(hibernateCallback); assertEquals(1, cities.size()); assertEquals("paris", cities.get(0).getName()); assertEquals("1", cities.get(0).getFeatureId() + ""); }
From source file:com.gisgraphy.hibernate.projection.ProjectionBeanTest.java
License:Open Source License
@SuppressWarnings("unchecked") @Test/* w w w . j a v a 2 s . c o m*/ public void testBeanFieldList() { City p1 = GisgraphyTestHelper.createCity("paris", 48.86667F, 2.3333F, 1L); this.cityDao.save(p1); HibernateCallback hibernateCallback = new HibernateCallback() { public Object doInHibernate(Session session) throws PersistenceException { try { Criteria testCriteria = session.createCriteria(City.class); String[] ignoreFields = { "distance" }; ProjectionList projection = ProjectionBean.beanFieldList(_CityDTO.class, ignoreFields, true); testCriteria.setProjection(projection); testCriteria.setResultTransformer(Transformers.aliasToBean(_CityDTO.class)); List<_CityDTO> results = testCriteria.list(); return results; } catch (HibernateException e) { fail("An exception has occured : maybe ignoreFields are not taken into account if the error is 'could not resolve property: distance... :" + e); throw e; } } }; List<_CityDTO> cities = (List<_CityDTO>) testDao.testCallback(hibernateCallback); assertEquals(1, cities.size()); assertEquals("paris", cities.get(0).getName()); assertEquals("1", cities.get(0).getFeatureId() + ""); }
From source file:com.gisgraphy.hibernate.projection.SpatialProjectionTest.java
License:Open Source License
@SuppressWarnings("unchecked") @Test/*from w ww . ja v a2 s .co m*/ public void testdistance_pointToLine() { LineString shape = GeolocHelper.createLineString("LINESTRING (6.9416088 50.9154239,6.9410001 50.9154734)"); OpenStreetMap streetOSM = GisgraphyTestHelper.createOpenStreetMapForPeterMartinStreet(); streetOSM.setShape(shape); openStreetMapDao.save(streetOSM); assertNotNull(openStreetMapDao.get(streetOSM.getId())); final Point p1 = GeolocHelper.createPoint(6.9412748F, 50.9155829F); HibernateCallback hibernateCallback = new HibernateCallback() { public Object doInHibernate(Session session) throws PersistenceException { Criteria testCriteria = session.createCriteria(OpenStreetMap.class); ProjectionList projection = Projections.projectionList() .add(Projections.property("name").as("name")).add(SpatialProjection .distance_pointToLine(p1, OpenStreetMap.SHAPE_COLUMN_NAME).as("distance")) .add(Projections.property("shape").as("shape")); // remove the from point testCriteria.setProjection(projection); testCriteria.setResultTransformer(Transformers.aliasToBean(_OpenstreetmapDTO.class)); List<_OpenstreetmapDTO> results = testCriteria.list(); return results; } }; List<_OpenstreetmapDTO> streets = (List<_OpenstreetmapDTO>) testDao.testCallback(hibernateCallback); assertEquals(1, streets.size()); Double calculatedDist = 14.76D; Double retrieveDistance = streets.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.gisgraphy.hibernate.projection.SpatialProjectionTest.java
License:Open Source License
@SuppressWarnings("unchecked") @Test//from www. j ava 2 s . co m @Ignore public void testDistance_sphere() { final City p1 = GisgraphyTestHelper.createCity("paris", 48.86667F, 2.3333F, 1L); City p2 = GisgraphyTestHelper.createCity("bordeaux", 44.83333F, -0.56667F, 3L); 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_sphere(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 = p1.distanceTo(p2.getLocation()); 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); }