Skip to content
Asterios edited this page Sep 15, 2016 · 4 revisions

##Hibernate Example

Here is an example with Hibernate and hql.

Get all adjacent areas around from the given geohash code: (How to create a geohash code you can see in the Simple example page.)



		Map<String, String> adjacentAreas = GeoHashUtils.getAllAdjacentAreasMap(geohash);


Build the hql statement:


		final StringBuilder hqlString = new StringBuilder();
				hqlString
				.append("select address.geohash from Addresses address " +
						"where address.geohash like :top " +
						"or address.geohash like :topright " +
						"or address.geohash like :right " +
						"or address.geohash like :bottomright " +
			        	"or address.geohash like :bottom " +
						"or address.geohash like :bottomleft " +
						"or address.geohash like :left " +
						"or address.geohash like :topleft " +
			        	"or address.geohash like :center " );

		String queryString = hqlString.toString();

		final Query query = getQuery(queryString);

Now add all query parameters to the query and get the result:

		for (Entry<String, String> entry : adjacentAreas.entrySet()) {
			query.setParameter(entry.getKey(), entry.getValue() + "%");
		}

		final List<Addresses> foundAddresses  = query.list();

I use this hql-statement in a subselect to limit the result:


		final StringBuilder hqlString = new StringBuilder();
		hqlString
		.append("select el from EventLocations el " +
				"where el.event.name like :eventname " +
				"and el.appointment.starttime between :start and :end " +
				"and el.userAddress.address.geohash in (" +
				// Subselect start...
				"select address.geohash from Addresses address " +
				"where address.geohash like :top " +
				"or address.geohash like :topright " +
				"or address.geohash like :right " +
				"or address.geohash like :bottomright " +
				"or address.geohash like :bottom " +
				"or address.geohash like :bottomleft " +
				"or address.geohash like :left " +
				"or address.geohash like :topleft " +
				"or address.geohash like :center " +
				// Subselect end...
				")");

The first query parameter defines the 'what' im looking for (in this case the eventname) The second query parameter defines the 'when' question (in this case the start and end time) and finally the last query parameter defines the 'where' question.(in this case with the geohash codes)

Note

If you use the new jgeohash version 2.x.x you have to use the new class GeoHashExtensions instead of GeoHashUtils.

Clone this wiki locally