Created
June 17, 2011 03:52
-
-
Save mpurbo/1030838 to your computer and use it in GitHub Desktop.
Hibernate Order extension sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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