List of usage examples for org.hibernate.criterion Projections property
public static PropertyProjection property(String propertyName)
From source file:com.gisgraphy.hibernate.criterion.NativeSQLOrderTest.java
License:Open Source License
@SuppressWarnings("unchecked") @Test/*from w ww.ja v a 2 s . c o m*/ public void testNativeSQLOrderShouldSortAscByDefault() { final City p1 = GisgraphyTestHelper.createCity("paris", 48.86667F, 2.3333F, 1L); City p2 = GisgraphyTestHelper.createCity("bordeaux", 44.83333F, -0.56667F, 3L); City p3 = GisgraphyTestHelper.createCity("goussainville", 49.01667F, 2.46667F, 2L); this.cityDao.save(p1); this.cityDao.save(p2); this.cityDao.save(p3); 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"); Projection projection = Projections.property("featureId").as("featureId"); testCriteria.setProjection(projection).addOrder(new NativeSQLOrder("featureId")) .setResultTransformer(Transformers.aliasToBean(_CityDTO.class)); List<_CityDTO> results = testCriteria.list(); return results; } }; List<_CityDTO> cities = (List<_CityDTO>) testDao.testCallback(hibernateCallback); assertEquals(3, cities.size()); assertEquals("1", cities.get(0).getFeatureId().toString()); assertEquals("2", cities.get(1).getFeatureId().toString()); assertEquals("3", cities.get(2).getFeatureId().toString()); }
From source file:com.gisgraphy.hibernate.criterion.NativeSQLOrderTest.java
License:Open Source License
@SuppressWarnings("unchecked") @Test//from w ww . ja v a 2 s. c o m public void testNativeSQLOrderShouldSortDesc() { final City p1 = GisgraphyTestHelper.createCity("paris", 48.86667F, 2.3333F, 1L); City p2 = GisgraphyTestHelper.createCity("bordeaux", 44.83333F, -0.56667F, 3L); City p3 = GisgraphyTestHelper.createCity("goussainville", 49.01667F, 2.46667F, 2L); this.cityDao.save(p1); this.cityDao.save(p2); this.cityDao.save(p3); 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"); Projection projection = Projections.property("featureId").as("featureId"); testCriteria.setProjection(projection).addOrder(new NativeSQLOrder("featureId", false)) .setResultTransformer(Transformers.aliasToBean(_CityDTO.class)); List<_CityDTO> results = testCriteria.list(); return results; } }; List<_CityDTO> cities = (List<_CityDTO>) testDao.testCallback(hibernateCallback); assertEquals(3, cities.size()); assertEquals("3", cities.get(0).getFeatureId().toString()); assertEquals("2", cities.get(1).getFeatureId().toString()); assertEquals("1", cities.get(2).getFeatureId().toString()); }
From source file:com.gisgraphy.hibernate.criterion.ProjectionOrderTest.java
License:Open Source License
@SuppressWarnings("unchecked") @Test/*from w w w. j a v a 2s . co m*/ public void testProjectionOrderShouldSortAscByDefault() { final City p1 = GisgraphyTestHelper.createCity("paris", 48.86667F, 2.3333F, 1L); City p2 = GisgraphyTestHelper.createCity("bordeaux", 44.83333F, -0.56667F, 3L); City p3 = GisgraphyTestHelper.createCity("goussainville", 49.01667F, 2.46667F, 2L); this.cityDao.save(p1); this.cityDao.save(p2); this.cityDao.save(p3); 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"); Projection projection = Projections.property("featureId").as("featureId"); testCriteria.setProjection(projection).addOrder(new ProjectionOrder("featureId")) .setResultTransformer(Transformers.aliasToBean(_CityDTO.class)); List<_CityDTO> results = testCriteria.list(); return results; } }; List<_CityDTO> cities = (List<_CityDTO>) testDao.testCallback(hibernateCallback); assertEquals(3, cities.size()); assertEquals("1", cities.get(0).getFeatureId().toString()); assertEquals("2", cities.get(1).getFeatureId().toString()); assertEquals("3", cities.get(2).getFeatureId().toString()); }
From source file:com.gisgraphy.hibernate.criterion.ProjectionOrderTest.java
License:Open Source License
@SuppressWarnings("unchecked") @Test//from w w w . j a v a 2s .c o m public void testProjectionOrderShouldSortDesc() { final City p1 = GisgraphyTestHelper.createCity("paris", 48.86667F, 2.3333F, 1L); City p2 = GisgraphyTestHelper.createCity("bordeaux", 44.83333F, -0.56667F, 3L); City p3 = GisgraphyTestHelper.createCity("goussainville", 49.01667F, 2.46667F, 2L); this.cityDao.save(p1); this.cityDao.save(p2); this.cityDao.save(p3); 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"); Projection projection = Projections.property("featureId").as("featureId"); testCriteria.setProjection(projection).addOrder(new ProjectionOrder("featureId", false)) .setResultTransformer(Transformers.aliasToBean(_CityDTO.class)); List<_CityDTO> results = testCriteria.list(); return results; } }; List<_CityDTO> cities = (List<_CityDTO>) testDao.testCallback(hibernateCallback); assertEquals(3, cities.size()); assertEquals("3", cities.get(0).getFeatureId().toString()); assertEquals("2", cities.get(1).getFeatureId().toString()); assertEquals("1", cities.get(2).getFeatureId().toString()); }
From source file:com.gisgraphy.hibernate.projection.ProjectionBean.java
License:Open Source License
/** * @param fields/* w w w . jav a 2s . c o m*/ * the list of the field names * @param autoAliasing * if the fields should be autoaliased. <b>See important note * above</b> */ public ProjectionBean(List<String> fields, boolean autoAliasing) { super(); for (String field : fields) { if (autoAliasing) { projectionList.add(Projections.property(field).as(field)); } else { projectionList.add(Projections.property(field)); } } }
From source file:com.gisgraphy.hibernate.projection.SpatialProjectionTest.java
License:Open Source License
@SuppressWarnings("unchecked") @Test//from www . jav a 2 s. c o 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 w w w . j a v a2 s . c o 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); }
From source file:com.gisgraphy.hibernate.projection.SpatialProjectionTest.java
License:Open Source License
@SuppressWarnings("unchecked") @Test/* w ww .j ava2s . c om*/ @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.github.javarch.repository.HibernateBaseRepositoryIntegrationTest.java
License:Apache License
@Test public void testLoadProjecao() { Projection p = Projections.projectionList().add(Projections.property("email"), "email"); List<User> users = defaultRepository.findAll(User.class, p); assertNotNull(users);/* ww w . ja v a2 s . com*/ for (User User : users) { assertNotNull(User.getEmail()); assertNull(User.getNomeUsuario()); assertNull(User.getSenha()); assertTrue(User.getBlogs().isEmpty()); } }
From source file:com.griffinslogistics.book.BookService.java
@Override public List<BookModel> getBookModelsByTransportId(Long transportId) { logger.log(Level.SEVERE, "{0}: getBookModelsByTransportation started", CLASS_NAME); List<BookModel> resultList = new ArrayList<BookModel>(); try {/* w w w . j a v a2s. c o m*/ DetachedCriteria criteria = DetachedCriteria.forClass(Book.class); criteria.add(Restrictions.eq("transportId", transportId)); criteria.setProjection( Projections.projectionList().add(Projections.groupProperty("bookNumber"), "bookNumber") .add(Projections.property("title"), "title")) .setResultTransformer(Transformers.aliasToBean(BookModel.class)); resultList = (List<BookModel>) this.dao.getAllByDetachedCriteria(criteria); Collections.reverse(resultList); } catch (HibernateException e) { logger.log(Level.SEVERE, e.getMessage()); } finally { logger.log(Level.SEVERE, "{0}: getBookModelsByTransportation finished", CLASS_NAME); } return resultList; }