I am trying to integrate hibernate spatial with JPA for Geo searches. I have been referencing the tutorial on the official site (I am not associated with hibernatespatial).
The tutorial, unfortunately, does not cover how to create a Point instance from a latitude/longitude pair. I'm attempting to do this here, but I am still not sure if this is this the right way to convert a latitude/longitude pair to a JTS Point instance:
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.hibernate.annotations.Type;
import javax.persistence.*;
@Entity
public class Location {
private Double latitude;
private Double longitude;
@Type(type = "org.hibernatespatial.GeometryUserType")
private Point coordinates;
private final GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
@PrePersist
@PreUpdate
public void updateCoordinate() {
if (this.latitude == null || this.longitude == null) {
this.coordinates = null;
} else {
this.coordinates = geometryFactory.createPoint(new Coordinate(latitude, longitude));
}
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
}