Skip to content

Instantly share code, notes, and snippets.

@mpurbo
Created June 17, 2011 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpurbo/1030838 to your computer and use it in GitHub Desktop.
Save mpurbo/1030838 to your computer and use it in GitHub Desktop.
Hibernate Order extension sample
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.CriteriaQuery;
import org.hibernate.criterion.Order;
public class PostgisDistanceOrder extends Order {
private boolean ascending;
private String propertyName;
private double fromLatitude;
private double fromLongitude;
public PostgisDistanceOrder(
double fromLatitude,
double fromLongitude,
String propertyName,
boolean ascending) {
super(propertyName, ascending);
this.fromLatitude = fromLatitude;
this.fromLongitude = fromLongitude;
this.ascending = ascending;
this.propertyName = propertyName;
}
@Override
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
throws HibernateException {
return "(ST_Distance(" +
criteriaQuery.getSQLAlias(criteria) + "." + propertyName + "," +
"ST_GeomFromText('POINT(" + fromLongitude + " " + fromLatitude + ")', 4326))) " +
(ascending ? " asc" : " desc");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment